Trying to upload a file with some parameters, I've changed the Ext upload plugin
to upload some files with post parameters.
The ext uploader
code (ux.upload.uploader.FormDataUploader) :
uploadItem : function(item) {
file = item.getFileApiObject();
item.setUploading();
formData = new FormData();
// mycode goes here to append some parameters
formData.append(file.name, file);
xhr = this.initConnection();
xhr.setRequestHeader(this.filenameHeader, file.name);
xhr.setRequestHeader(this.sizeHeader, file.size);
xhr.setRequestHeader(this.typeHeader, file.type);
var loadendhandler = Ext.Function.bind(this.onLoadEnd, this, [
item
], true);
var progresshandler = Ext.Function.bind(this.onUploadProgress, this, [
item
], true);
xhr.addEventListener('loadend', loadendhandler, true);
xhr.upload.addEventListener("progress", progresshandler, true);
xhr.send(formData);
},
initConnection : function() {
var xhr = new XMLHttpRequest(),
method = this.method,
url = this.url;
xhr.open(method, url, true);
this.abortXhr = function() {
this.suspendEvents();
xhr.abort();
this.resumeEvents();
};
return xhr;
},
I tried these two lines of my code:
formData.append("ali","ghasemi");
formData.append("alisd","ghassdf");
But the parameters were added into the request payload in the following manner:
------WebKitFormBoundarylAKSY21NN15kqWct
Content-Disposition: form-data; name="ali"
ghasemi
------WebKitFormBoundarylAKSY21NN15kqWct
Content-Disposition: form-data; name="alisd"
ghassdf
------WebKitFormBoundarylAKSY21NN15kqWct
Content-Disposition: form-data; name="icon-info.gif"; filename="icon-info.gif"
Content-Type: image/gif
GIF89a
How can I get the post
parameters at server side or how set these parameters correctly at the client side?
I am coding in java spring. HttpServletRequest
does not know these params.
public JSONObject upload(HttpServletRequest request, HttpServletResponse response, @RequestBody String body) throws IOException {
String ali = request.getParameter("ali"); // returns null
}
Any answer in Ext or pure javascript or Java spring controller
will be appreciated.
Here's a related question: formdata-appendkey-value-is-not-working