ProgerXP / FileDrop

Self-contained cross-browser pure JavaScript class for Drag & Drop and AJAX (multi) file upload.
filedropjs.org
The Unlicense
264 stars 61 forks source link

File without parameter name in post request #63

Open parnager opened 7 years ago

parnager commented 7 years ago

Hi, thanks for your work, FileDrop works nice all in all. But I have a problem with the Request. My Server requires a param name for the file, such as ID__file and the binary data connected to that. Otherwise I found no possibility, to read the file contents and read it in Java. Is there any possibility (without using the iframe solution) to give the file in the post request a custom parameter name? I already used iframe.fileParam, but as the name says, it's not really what I need.

If there is no possibility, do you have any ideas or examples, how to read the file data with a java servlet?

In the image below, i have on the upper side a screenshot of our old, working request. On the lower side there is the request sent by FileDrop, where I passed some parameters in the url (working) but the file data is just located in the post request.

I would be grateful for any ideas.

gutschlecht

kind regards, Bernhard

ProgerXP commented 7 years ago

Is there any possibility (without using the iframe solution) to give the file in the post request a custom parameter name?

No, this is browser's limitation. FileDrop was written to support quite old browsers and by that time the spec was implemented so that if you want to send a file chosen by the user (via the open file dialog) then you have to send it as a complete POST data, you can't customize it in any way. If you look at the source code, you will see something like xmlHttpRequest.send(fileObject) - in contrast to how you normally submit POST XHR (a=b&c=d etc.).

With modern browsers that properly implement File API (most of them, even IE to some point) it's possible to work with file objects directly, i.e. read their data from any offset and of any length. Therefore you can read entire file into a string and construct a normal multipart POST data as you would like. It will work for small files all right but not for big files because it'll eat up memory and be slow.

Using xhr.send(file) is still the most straightforward way because 1) old browsers support it, 2) browser handles memory and all other issues itself. The only downside is that you can't control the form of POST data exactly. In PHP and ASP you have access to raw POST data, don't you have it in JSP?

It's still possible to add custom request parameters to this kind of request, just use GET query part (as seen on some FileDrop demos), i.e. add ?foo&bar... to the upload script URL.