19 ottobre 2010

Video upload: Plupload with ASP.Net 3.5

Recently I'm moving from swfupload because even if this is a great tool I'm needing to let my users upload larger file.

So I'm testing Plupload: this uploader supports HTML5, Gears, Silverlight, Flash, BrowserPlus and chunked uploads.


Server side I'm using ASP.Net so thanks to Alan for this post that helped me on start... but I must edit his code because the final file results were always corrupted because the first bytes of it was as this example lines:

------pluploadboundary634230768900613890
Content-Disposition: form-data; name="file"; filename="file.wmv"
Content-Type: application/octet-stream

This is because Alan reads from Request.InputStream and not from Request.Files that it isn't empty like he thinks.

My final code that does this is:

int chunk = Request.QueryString["chunk"] != null ? int.Parse(Request.QueryString["chunk"]) : 0;
 

string fileName = Request.QueryString["name"] != null ? Request.QueryString["name"] : string.Empty;

HttpPostedFile fileUpload = Request.Files[0];       
               
using (FileStream fs = new FileStream(Server.MapPath("/public/" + fileName), chunk == 0 ? FileMode.Create : FileMode.Append))
{           
    Byte[] buffer = new Byte[fileUpload.InputStream.Length];
    fileUpload.InputStream.Read(buffer, 0, buffer.Length);
          
    fs.Write(buffer, 0, buffer.Length);           
    fs.Close();
}


This works great with larger files up to 2GB with Silverlight as runtime: if I try also with a file of 6GB the upload stops to 2GB even if it is segmented by 1MB chunks... and I don't know why?!