I cannot (afaik) create a file in my (javascript browser) client, so I want my server to do it before sending it back to the client which in turn should open a dialogue asking the user where to save the file.
How do I achieve this? Se below for a gist of what I currently have.
Client
$.ajax({
type: 'POST',
dataType: 'json',
url: "http://localhost:5000/api/file",
data: postData,
contentType : 'application/json',
success: function (responseData, textStatus, jqXHR) {
// What do I put here?
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
Server
router.post('/file', function (request, response) {
var content = '';
request.on('data', function (data) {
content += data;
});
request.on('end', function () {
var contentAsJson = JSON.parse(content);
response.set("Content-Type", "text/plain");
response.set("Content-Length", contentAsJson.text.length);
response.set("Content-Disposition", 'attachment; filename='filename.txt');
response.send(contentAsJson.text);
});
});