Documentation > CMS Template API Library > Asset > SaveUploadedFile(String,UploadedFile)

SaveUploadedFile

Save an uploaded file to an asset. Usually used in upload.aspx to save the original image before creating a thumbnail. Import Note: This would be called from upload.aspx when using Input.ShowAcquireImage() to upload an image. You need to provide this key to the ShowAcquireImage call in the optional "hiddenFields" parameter, otherwise things may not save correctly. When calling from upload.aspx, saving is not immediate. Data is stored temporarily and is saved when the form is committed.

public System.Void SaveUploadedFile(String,UploadedFile)

Parameters

NameDescriptionType
key The name to save it under. Don't include "upload#". If null or an empty string is passed, we will use the key in the object. System.String
uploadedFile The uploaded file. CrownPeak.CMSAPI.UploadedFile

Code Example

C#

Sample:
UploadedFile imageUpload = context.UploadedFile;
]string value = imageUpload.Path;
// my_photo_thumbnail was used in input.aspx because we want to display the thumbnail when we edit, not the possibly large original
if (imageUpload.Matches("my_photo_thumbnail"))
{
  asset.SaveUploadedFile("my_photo", imageUpload); // it gets overwritten later when it is resized to 300x300
  Img img = Img.Load(value);
  if (!img.HasError)
  {
    ImgResult res = img.CreateThumbnail("my_photo_thumbnail", 300, 300);
    if (res.HasError)
    {
      context.Error = "Upload Error creating thumbnail on " + value + " error: " + res.ErrorMessage;
    }
  }
  else
  {
    context.Error = "Upload Error Loading on " + value + " error: " + img.ErrorMessage;
  }
}    

Connect with Crownpeak