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?!

2 commenti:

  1. Hi Marco,

    I'm trying to integrate the Plupload file uploader in ASP.NET using C# as well and I have a couple questions.

    First, how do you configure the uploader? I have:

    $("#plupload_container").pluploadQueue({
    runtimes: 'html5,gears,flash,silverlight,html4',
    flash_swf_url: '../plupload/js/plupload.flash.swf',
    silverlight_xap_url: '../plupload/js/plupload.silverlight.xap',
    filters: [
    { title: "Image files", extensions: "jpg,gif" },
    { title: "Zip files", extensions: "zip" },
    { title: "Document files", extensions: "doc,pdf,txt" }
    ]
    });
    I guess my main question is how do I call the code you posted so that the uploading can begin? I have a form on a page SubmitRequest.aspx and I have it set up so that when you click 'Submit' on the form I do:

    $('form').submit(function (e) {

    // Validate number of uploaded files
    if (uploader.total.uploaded == 0) {
    // Files in queue upload them first
    if (uploader.files.length > 0) {
    // When all files are uploaded submit form
    uploader.bind('UploadProgress', function () {
    if (uploader.total.uploaded == uploader.files.length)
    $('form').submit();
    });

    uploader.start();
    }
    e.preventDefault();
    }
    });

    ... but where do I define the method that will execute your code? Do I need to define a new .aspx file that will take care of this? (I don't like this approach).

    Simply put, can you just walk me through the process of logic from setting up the uploader to when the C# code executes? I appreciate your time!

    Thanks,
    Hristo Oskov

    RispondiElimina
  2. Thanks for the code, it saved me ..exactly working as expected even for the bigger size files.

    RispondiElimina