admin管理员组文章数量:1026989
I am writing HTML WYSIWYG application. After a user edits a web page, they should be able to click a download button, and be prompted by their browser to save their edited web page. I do not want to store their edited web page on my server, but send the file to their local machine.
So far in my application, I am able to edit a template, grab its HTML when the user clicks the download button, and using NodeJS's fs.createWriteStream method, write the HTML to a newly created HTML file. However, it creates the file on my server instead of prompting the browser to save the file in a Downloads folder. I am using Socket.io to tell the server to download the HTML content.
After research, I figured out that in order to make the browser prompt the user to save a download file, the HTTP headers must be set using the Content-Disposition: attachment header.
In Summary, when the user clicks the download button in my application, what should I do so that a new file is created and saved directly on the users local puter instead of on my server?
This is what I have in my server.js file:
io.sockets.on('connection', function(socket) {
socket.on('downloadHTML', function(data) {
var file = fs.createWriteStream('PearlEdit-template-Martketing.html', {flags: 'w'})
.write(data.html);
});
});
I am writing HTML WYSIWYG application. After a user edits a web page, they should be able to click a download button, and be prompted by their browser to save their edited web page. I do not want to store their edited web page on my server, but send the file to their local machine.
So far in my application, I am able to edit a template, grab its HTML when the user clicks the download button, and using NodeJS's fs.createWriteStream method, write the HTML to a newly created HTML file. However, it creates the file on my server instead of prompting the browser to save the file in a Downloads folder. I am using Socket.io to tell the server to download the HTML content.
After research, I figured out that in order to make the browser prompt the user to save a download file, the HTTP headers must be set using the Content-Disposition: attachment header.
In Summary, when the user clicks the download button in my application, what should I do so that a new file is created and saved directly on the users local puter instead of on my server?
This is what I have in my server.js file:
io.sockets.on('connection', function(socket) {
socket.on('downloadHTML', function(data) {
var file = fs.createWriteStream('PearlEdit-template-Martketing.html', {flags: 'w'})
.write(data.html);
});
});
Share
Improve this question
asked Jan 2, 2014 at 6:46
RashadRashad
2456 silver badges15 bronze badges
2
- 2 Just serve the file with the proper headers when the link is clicked, don't use websockets for that – adeneo Commented Jan 2, 2014 at 6:48
- I'm not 100% sure what you mean. Could you elaborate? – Rashad Commented Jan 2, 2014 at 8:12
1 Answer
Reset to default 3You can simply use something like this (answer referenced from here)
app.get('/download', function(req, res){
var file = __dirname + '/upload-folder/dramaticpenguin.MOV';
var filename = path.basename(file);
var mimetype = mime.lookup(file);
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);
var filestream = fs.createReadStream(file);
filestream.pipe(res);
});
I am writing HTML WYSIWYG application. After a user edits a web page, they should be able to click a download button, and be prompted by their browser to save their edited web page. I do not want to store their edited web page on my server, but send the file to their local machine.
So far in my application, I am able to edit a template, grab its HTML when the user clicks the download button, and using NodeJS's fs.createWriteStream method, write the HTML to a newly created HTML file. However, it creates the file on my server instead of prompting the browser to save the file in a Downloads folder. I am using Socket.io to tell the server to download the HTML content.
After research, I figured out that in order to make the browser prompt the user to save a download file, the HTTP headers must be set using the Content-Disposition: attachment header.
In Summary, when the user clicks the download button in my application, what should I do so that a new file is created and saved directly on the users local puter instead of on my server?
This is what I have in my server.js file:
io.sockets.on('connection', function(socket) {
socket.on('downloadHTML', function(data) {
var file = fs.createWriteStream('PearlEdit-template-Martketing.html', {flags: 'w'})
.write(data.html);
});
});
I am writing HTML WYSIWYG application. After a user edits a web page, they should be able to click a download button, and be prompted by their browser to save their edited web page. I do not want to store their edited web page on my server, but send the file to their local machine.
So far in my application, I am able to edit a template, grab its HTML when the user clicks the download button, and using NodeJS's fs.createWriteStream method, write the HTML to a newly created HTML file. However, it creates the file on my server instead of prompting the browser to save the file in a Downloads folder. I am using Socket.io to tell the server to download the HTML content.
After research, I figured out that in order to make the browser prompt the user to save a download file, the HTTP headers must be set using the Content-Disposition: attachment header.
In Summary, when the user clicks the download button in my application, what should I do so that a new file is created and saved directly on the users local puter instead of on my server?
This is what I have in my server.js file:
io.sockets.on('connection', function(socket) {
socket.on('downloadHTML', function(data) {
var file = fs.createWriteStream('PearlEdit-template-Martketing.html', {flags: 'w'})
.write(data.html);
});
});
Share
Improve this question
asked Jan 2, 2014 at 6:46
RashadRashad
2456 silver badges15 bronze badges
2
- 2 Just serve the file with the proper headers when the link is clicked, don't use websockets for that – adeneo Commented Jan 2, 2014 at 6:48
- I'm not 100% sure what you mean. Could you elaborate? – Rashad Commented Jan 2, 2014 at 8:12
1 Answer
Reset to default 3You can simply use something like this (answer referenced from here)
app.get('/download', function(req, res){
var file = __dirname + '/upload-folder/dramaticpenguin.MOV';
var filename = path.basename(file);
var mimetype = mime.lookup(file);
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);
var filestream = fs.createReadStream(file);
filestream.pipe(res);
});
本文标签: javascriptDownload file in NodeJSStack Overflow
版权声明:本文标题:javascript - Download file in NodeJS - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745602314a2158528.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论