Thursday, March 31, 2016

How do I prevent an ASPxUploadControl from breaking when I upload something too big.

You can upload the image in chunks and if the chunks exceed a limit then you can proactively freak out ahead of things getting really bad.

<dx:ASPxUploadControl id="Foo" runat="server" ClientIDMode="Static"
      ClientInstanceName="Bar" OnFileUploadComplete="Foo_FileUploadComplete"
      UploadMode="Advanced">
   <ValidationSettings MaxFileSize="1048576" MaxFileSizeErrorText="Fail!"/>
   <AdvancedModeSettings PacketSize="131072"></AdvancedModeSettings>
</dx:ASPxUploadControl>

 
 

This magic happens in an async-under-the-hood manner and not on a post of a form. Start an upload like so from JavaScript:

Bar.Upload();

 
 

At the code behind we need...

protected void Foo_FileUploadComplete(object send, FileUploadCompleteEventArgs e)
{
   byte[] bytes = new byte[e.UploadedFile.FileContent.Length];
   using (Stream fileStream = e.UploadedFile.FileContent)
   {
      fileStream.Read(bytes, 0, (int) fileStream.Length);
   }
   DoWhatever(bytes);
}

No comments:

Post a Comment