Search code examples
javascripthttpwebrequestmultipartform-dataweb-application-design

request parameters ordering undefined [in multipart/form-data or in general] - what to do?


I am writing a web application that submits a form (one of its fields is mulitpart/form-data, so obviously POST must be used and not GET, since the files might be really big). One of the fields is kinda transaction/upload_id and the other is obviously the file contents. While uploading, a progress bar must be displayed.

The known fact says that the order of parameters is undefined in general, meaning that any of (file content / upload_id) might come first.

Is there any acceptable / recommended way to cause the browser to send the upload_id before sending the file content?

Is it a considered a correct implementation - to expect the upload_id to come first OR there is a better / most common / more correct way to handle the problem? In that case - it would be fantastic to hear some details.

Update: my server-side language is Java/Servlets 3.0


Solution

  • Using servlets as well, and in my case I wanted to run my CSRF filter in my servlet before I started streaming the file: if the filter failed, I can kill the request before I've uploaded my 20gb video file, as opposed to the default PHP implementation where the server only hits your script AFTER its parsed the entire request.

    Its been a bit of a hack on my part, but in the couple of cases I've had to do this I've cheated and put the non-file request parameters into the URL and in every case (using pretty much every browser I've tested with) an Iterator over the request parameters on the server (I'm using commons fileupload in streaming mode) has received the non-file request parameters first before the file data was received. Somewhat fragile, but not unworkable.

    I'm assuming that if you order your request parameters with the file <input> as the last item you'll get the same behavior.