I'm trying to download a binary file using XMLHttpRequest and store its content. The URL im querying returns the PDF data. This is how the request to the server and response looks like :
POST /download.php HTTP/1.1
[....]
id=1&file_type=pdf
HTTP/1.1 200 OK
Server: Apache
Content-Disposition: attachment; filename="file_1.pdf";filename*=ut8'en'file_1.pdf
X-Cnection: close
Content-Type: application/octet-stream
[.....]
Connection: keep-alive
%PDF-1.3
[raw PDF data]
I tried this code and i still get nothing but a blank response :
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://foo.example.com/download.php', true);
//xhr.overrideMimeType("text/plain; charset=x-user-defined");
//xhr.responseType = 'arraybuffer'; // blob too
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var file = xhr.response // this.response
// store it somewhere ..
}
}
xhr.send("id=1&file_type=pdf");
}
Note : The request is sent from a page in : www.example.com
Is there any kind of trick to do it via XHR ? How would you achieve this yourself?
Any help or hint is much appreciated :)