Help us to translate this website and improve this translation!
English | Castellano

How to Use the FileUpload AJAX Control

Managing the HttpPostedFileAJAX

Up to now, we have reviewed many of the properties of FileUpload AJAX... but when we are going to save a file? how can we apply a filter for the maximum file size? how can I know when a file is uploading or is simply a normal call?

Now that we know what the HttpPostedFileAJAX is, we can use the properties and methods that are available within the control:
  • IsRequesting: indicates if a request due to FileUpload AJAX has been initiated. The request can be initiated when a file is uploading or when it is being erased.
  • IsPosting: indicates that a file from the FileUpload AJAX control is uploading.
  • IsDeleting: indicates that a file is being deleted by FileUpload AJAX.
  • PostedFile: provides access to the HttpPostedFileAJAX that FileUpload AJAX uploaded.
  • Saving the PostedFile: we have 3 options when we have uploaded the PostedFile, none of which involve using "server.MapPath(...)", because the control does it automatically:
    • SaveAs(string path): save the file to the specified path (directory + file name). The results are identical to the SaveAs command in the standard FileUpload control.
    • Save(): save the file in the root of our application with the original name.
    • SaveAs(string directoryPath, string FileName): save the file in the specified directory (directoryPath) and with the specified name (FileName). A function that is missing from the standard FileUpload control.
In the example we are going to save a file that must be a GIF image that is less than 5KB in the "temp" directory located in the root of our application.


<cc1:FileUploaderAJAX ID="FileUploaderAJAX1" runat="server" MaxFiles="5" />
protected void Page_Load(object sender, EventArgs e)
   if (FileUploaderAJAX1.IsPosting)

private void managePost()
   HttpPostedFileAJAX pf = FileUploaderAJAX1.PostedFile;

   if ((pf.Type == HttpPostedFileAJAX.fileType.image) && pf.ContentLength <= 5 * 1024)
      FileUploaderAJAX1.SaveAs("~/temp", pf.FileName);