Khan
Khan

Reputation: 2982

Why is Request.Files.Count sometimes 0 using HTML5 uploader?

We're using plupload for users to upload files to our VPS.

Here is the plupload code:

 var uploader = new plupload.Uploader({
    browse_button: 'fileSelectorLink',
    container: 'uploadContainer',
    drop_element: 'uploadbox',
    url: '/UploadHandler.ashx',
    unique_names: true,
    multi_selection: false,
    flash_swf_url: '/scripts/plupload/js/Moxie.swf',
    silverlight_xap_url: '/scripts/plupload/js/Moxie.xap'
});

He is the code for processing the request:

Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest
  Dim runtime As String = If(context.Request("runtime") IsNot Nothing, context.Request("runtime"), "Unknown")
  If context.Request IsNot Nothing Then
    If context.Request.Files.Count > 0 Then
       Dim fileUpload As HttpPostedFile = context.Request.Files(0)
       ...
    Else
       ' Throw an exception
  End If
End Sub

Occasionally (about 2% of the time) the exception will get thrown. Here is an example of request that threw an error:

Runtime: html5
Request.ContentType: multipart/form-data; boundary=----WebKitFormBoundaryo7JAtlhKsg8xDcQT
Request.ContentLength: 3758089
Request.ContentEncoding: System.Text.SBCSCodePageEncoding
Request.TotalBytes: 3179067

The errors seem to happen across different browsers and OSs (even modern browsers), so that doesn't seem to be the issue. Plupload should fall back to other versions if browser doesn't handle async file uploads. I thought the ContentEncoding seemed odd, but it seems to always say that (maybe plupload works like that?). The only thing that jumped out at me is that the ContentLength and TotalBytes were different, but in my local testing they were the same. Could that be a problem?

Been stuck on this issue for a few days and haven't had any good leads.

Upvotes: 4

Views: 1026

Answers (1)

NoAlias
NoAlias

Reputation: 9193

The failures could be occurring when users abort the upload (usually indicated by "The remote host closed the connection." exceptions), are encountering network latency, perhaps from HTTP timeouts, maybe even an issue with the Plupload code. My apologies for not being specific with the reason, however I believe I can make up for it by providing a solution that could limit issues encountered by users on your site.

Plupload contains a setting that will retry the upload upon encountering a HTTP error response. Use the max_retries setting so that in an instance where there is a "network hiccup", another attempt (or few) is/are done to retry the upload. Of course you'll still see the exceptions, but this may prevent the users from having to click the upload button again.

Your code would then become:

  var uploader = new plupload.Uploader({
    browse_button: 'fileSelectorLink',
    max_retries: 2,
    container: 'uploadContainer',
    drop_element: 'uploadbox',
    url: '/UploadHandler.ashx',
    unique_names: true,
    multi_selection: false,
    flash_swf_url: '/scripts/plupload/js/Moxie.swf',
    silverlight_xap_url: '/scripts/plupload/js/Moxie.xap'
});

Additionally, you can use a binary stream instead of multipart to post the file, which may alleviate issues with older browsers. You control this with the multipart setting.

Upvotes: 4

Related Questions