Using the XMLHttpRequest Submission File with Data

I am trying to ajaxlly upload a file, including I want to add message data with it

var xhr = this._xhrs[id] = new XMLHttpRequest();
var queryString = qq.obj2url(params, this._options.action);
        xhr.open("POST", queryString, true);
        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        xhr.setRequestHeader("X-File-Name", encodeURIComponent(name));
        xhr.setRequestHeader("Content-Type", "application/octet-stream");
        xhr.send(file);

How can I add x=ya message as data?

+3
source share
2 answers

You cannot publish a file and form data as x = y & w = z. These are two different types of content.

For x = y, you should use a content type, for example: application / x-www-form-urlencoded

I suggest you split the AJAX request into two different ones or paste this data into the url: myUrl + 'query.php? x = y '.

Ciao

Wilk

+3
source
var file = $("#file_input")[0].files[0];

var formData = new FormData();
formData.append("myfile", file);
formData.append("text_unput", "Hello");

var xhr = new XMLHttpRequest();
xhr.open('POST', '/url', true);
xhr.send(formData);
+1
source

All Articles