Trying to send simple text string as a file, that being downloaded under certain name upon request. Can't seems to figure out why this code fails.
尝试将简单文本字符串作为文件发送,根据请求以特定名称下载。似乎无法弄清楚为什么这段代码失败了。
var text_ready = "This is a content of a txt file."
res.setHeader('Content-type', "application/octet-stream");
res.setHeader('Content-disposition', 'attachment; filename=file.txt');
res.send( new Buffer(text_ready) );
When this code executes, I receive only a XHR response (with that string as a content), but no download is being initiated. But I expected that receiveing this response will force browser to download a file with file.txt
as a name having content of the string above.
当此代码执行时,我只收到一个XHR响应(该字符串作为内容),但没有启动下载。但我希望收到此响应将强制浏览器下载一个文件,其中file.txt作为具有上述字符串内容的名称。
How to fix that? What am I doing wrong?
如何解决?我究竟做错了什么?
Maybe it will be important: working under Chrome 41 on Windows.
也许这很重要:在Windows上的Chrome 41下工作。
EDIT: it seems that I have to describe a bit deeper. The workflow is the following:
编辑:似乎我必须更深入地描述。工作流程如下:
- page contains a table cells with angular
ng-click
events attached to each of them - when clicking a GET request is being sent to "/download" using jQuery's $.get("/download")
- server have route to handle this GET requests
- what I need to achieve is that a certain text string is being send to user and saved as a file (so that a download is being initiated upon click, shortly speaking)
页面包含一个表格单元格,每个表格单元格都附有角度ng-click事件
单击GET请求时使用jQuery的$ .get(“/ download”)发送到“/ download”
服务器有路由来处理此GET请求
我需要实现的是将某个文本字符串发送给用户并保存为文件(以便在点击时启动下载,很快就会说)
testings
- It works when I navigate manually to that url by entering this url to address bar and pressing enter
- It does not work when I click with mouse on that cell - the request is sent, the response is received but no download is initiated.
当我通过输入此URL到地址栏并按Enter键手动导航到该URL时,它可以工作
当我在该单元格上单击鼠标时它不起作用 - 发送请求,收到响应但不启动下载。
1 个解决方案
#1
10
Tested on my node.js version 0.12.2, Windows 7, Chrome and Firefox
在我的node.js版本0.12.2,Windows 7,Chrome和Firefox上测试过
var http = require('http');
http.createServer(function (req, res) {
var text_ready = "This is a content of a txt file."
res.writeHead(200, {'Content-Type': 'application/force-download','Content-disposition':'attachment; filename=file.txt'});
res.end( text_ready );
}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/');
I think that you're not running the latest version of node.js. Maybe you can change only Content-type header, which might work. Also, you can use res.end( new Buffer(text_ready) );
. I've tested it.
我认为你没有运行最新版本的node.js.也许您只能更改Content-type标头,这可能有用。此外,您可以使用res.end(new Buffer(text_ready));.我测试过了。
EDITED: How to download file from node.js with javascript(jQuery onclick) call:
编辑:如何使用javascript(jQuery onclick)调用从node.js下载文件:
Javascript:
<script type="text/javascript">
$(document).ready(function(){
$("#button").on('click',function(){
window.location = 'http://127.0.0.1:1337/';
});
});
</script>
HTML:
<button id="button">Download</button>
I'm sure you know how to rewrite from jQuery to angularJS. :) I think that this is better than using an Ajax call. If you really need Ajax, I'll find out how to do it.
我相信你知道如何从jQuery重写为angularJS。 :)我认为这比使用Ajax调用更好。如果你真的需要Ajax,我会发现如何做到这一点。
#1
10
Tested on my node.js version 0.12.2, Windows 7, Chrome and Firefox
在我的node.js版本0.12.2,Windows 7,Chrome和Firefox上测试过
var http = require('http');
http.createServer(function (req, res) {
var text_ready = "This is a content of a txt file."
res.writeHead(200, {'Content-Type': 'application/force-download','Content-disposition':'attachment; filename=file.txt'});
res.end( text_ready );
}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/');
I think that you're not running the latest version of node.js. Maybe you can change only Content-type header, which might work. Also, you can use res.end( new Buffer(text_ready) );
. I've tested it.
我认为你没有运行最新版本的node.js.也许您只能更改Content-type标头,这可能有用。此外,您可以使用res.end(new Buffer(text_ready));.我测试过了。
EDITED: How to download file from node.js with javascript(jQuery onclick) call:
编辑:如何使用javascript(jQuery onclick)调用从node.js下载文件:
Javascript:
<script type="text/javascript">
$(document).ready(function(){
$("#button").on('click',function(){
window.location = 'http://127.0.0.1:1337/';
});
});
</script>
HTML:
<button id="button">Download</button>
I'm sure you know how to rewrite from jQuery to angularJS. :) I think that this is better than using an Ajax call. If you really need Ajax, I'll find out how to do it.
我相信你知道如何从jQuery重写为angularJS。 :)我认为这比使用Ajax调用更好。如果你真的需要Ajax,我会发现如何做到这一点。