I have html webpage with javascript script that uses XMLHttpRequest to get a file from a server. I want a browser to open a standard "download file" window, so a user can save a file. The code for that is simple:
var xhr;
xhr=new XMLHttpRequest();
xhr.onreadystatechange=function()
{
if (xhr.readyState==4 && xhr.status==200)
{
alert("File successfully received");
}
}
xhr.open("GET”,”http://localhost:17001/filedl?param=1",true);
xhr.send();
On a server, I have a running node.js app with express module. The app is generating a buffer data and sending it back to the client as a file. The code is here:
var express = require('express');
var app = express();
app.get('/filedl', function(req, res){
var pngBuffer = new Buffer("generated png goes here");
res.setHeader('Content-Description','File Transfer');
res.setHeader('Content-Disposition', 'attachment; filename=print.png');
res.setHeader('Content-Type', 'application/octet-stream');
res.end(pngBuffer);
});
app.listen(17001);
However, I can't get a "Save As" dialog open up in a browser. I'm just getting a response from my server with a buffer in a text form.
I've tried setting different response http headers, but without any luck.
var blobObject = new Blob([xhr.respomse], {type: 'image/png'});
navigator.msSaveBlob(blobObject, "print.png");
blobObject.msClose();
I'm using navigator.msSaveBlob function on client side to save a file in Internet Explorer, hope that works for you