Skip to main content

Upload Large Files

  • 4 minutes to read

This topic describes ASPxUploadControl customization options that are available when large files are uploaded. The specific customization technique depends on the upload mode defined for ASPxUploadControl by the ASPxUploadControl.UploadMode property. To learn more on upload mode selection, refer to the following topic: Upload Modes.

Standard Upload Mode

ASPxUploadControl works in Standard Upload Mode if its ASPxUploadControl.UploadMode property is set to UploadControlUploadMode.Standard, or if the ASPxUploadControl.UploadMode property is set to UploadControlUploadMode.Auto and neither Html5 nor Silverlight technology is available.

In this mode, you can configure ASPxUploadControl to upload large files as described in the following Microsoft topics:

Configure the following options to support the upload of large files:

  • maxRequestLength - in the Web.Config file

    Modify the httpRuntime element’s maxRequestLength configuration setting (whose default value is 4096 in kilobytes) to increase the Request length limit to the largest file upload size allowed for your application. This specifies the total size of all files that are selected within an ASPxUploadControl and uploaded in a single request. If the total size of the uploaded file/files is larger than maxRequestLength, the upload is canceled and the following exception occurs: “The server encountered an internal unspecified error that prevented it from fulfilling the request.”

    <system.web>
         <httpRuntime maxRequestLength="30000" />
         ...
    
  • maxAllowedContentLength (IIS7 specific) - in the Web.Config file

    For IIS7, in addition to maxRequestLength, you need to customize the requestLimits element’s maxAllowedContentLength configuration setting (its default value is 30000000, in bytes).

    <system.webServer>
         <security>
              <requestFiltering>
                   <requestLimits maxAllowedContentLength="30000000">
                   </requestLimits>
              </requestFiltering>
         </security>
         ...
    
  • MaxFileSize - in ASPxUploadControl

    If you define file validation settings and specify the UploadControlValidationSettings.MaxFileSize property of ASPxUploadControl, ensure that the Request length limit defined by Web.Config options (maxRequestLength, maxAllowedContentLength) is not less than the MaxFileSize property value. The default value of MaxFileSize is 0, which means that ASPxUploadControl does not validate file size by itself.

Note that in Standard Upload Mode, the maximum file size that can be uploaded is approximately 1GB. Additionally, ASPxUploadControl requires memory for upload progress visualization. Therefore in Standard Upload Mode, the actual maximum file size that can be uploaded using ASPxUploadControl with upload progress visualization enabled (the ASPxUploadControl.ShowProgressPanel property is set to true) is approximately 2GB/3 = 633MB. In this case, if you try to upload a larger file, you will see the server-side System.OutOfMemoryException exception.

Advanced Upload Mode

ASPxUploadControl works in Advanced Upload Mode if its ASPxUploadControl.UploadMode property is set to UploadControlUploadMode.Advanced, or if the ASPxUploadControl.UploadMode property is set to UploadControlUploadMode.Auto and either Html5 or Silverlight technology is available.

This mode is more suitable for uploading large files, because a file is uploaded in small packets, and is not cached in its entirety in server memory. The available Advanced Upload Mode customization capabilities are listed below.

  • File Packet Size

    Use ASPxUploadControl’s UploadAdvancedModeSettings.PacketSize property to specify the packet size. Its default value is 200000 bytes. If the control’s progress panel is enabled (the ASPxUploadControl.ShowProgressPanel property is set to true), the upload progress indicator is updated after each file packet is uploaded.

    You can increase the packet size if large files (10MB and larger) will be uploaded and a high-speed Internet connection is used (for instance, when using Intranet applications). Conversely, you should decrease the packet size when most files are small and there is limited bandwidth.

  • Temporary File Folder

    In Advanced Upload Mode, ASPxUploadControl saves each uploaded file into a temporary file (with the .tmp extension) within a specific server folder. To specify this folder, use the UploadAdvancedModeSettings.TemporaryFolder property (the default path is ~\App_Data\UploadTemp\). Ensure that the ASP.NET application has ‘write’ access to the folder on the server. See the following Microsoft topic for information on how to grant ‘write’ access to a web application: FileUpload Class: Security Considerations.

  • Access to Uploaded File

    After ASPxUploadControl has finished uploading a file into a temporary file, you can access the file settings through properties of the file-related UploadedFile object instance. To obtain this object, use the FileUploadCompleteEventArgs.UploadedFile property (when you handle the ASPxUploadControl.FileUploadComplete event) or use the ASPxUploadControl.UploadedFiles property (when you handle the ASPxUploadControl.FilesUploadComplete event).

    For large files, you should access the uploaded file content using the UploadedFile.FileContent property of the UploadedFile, and not from UploadedFile.FileBytes - to avoid server memory overflow. The UploadedFile.FileContent property returns file content as a file stream that you should then delete. Thus, it is necessary to manage the uploaded file’s stream within the using statement (Using statement in Visual Basic).

    protected void ASPxUploadControl1_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e) {
            using(Stream stream = e.UploadedFile.FileContent)  {
                stream.Read(...);
            }
        }
    

    The simplest way to save the uploaded file is to use the UploadedFile.SaveAs method.

    protected void ASPxUploadControl1_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e) {
                string filePath = Server.MapPath("~/Uploads/" + e.UploadedFile.FileName);
                e.UploadedFile.SaveAs(filePath);
                // Process the saved file at filePath.
        }