简体   繁体   English

如何在nodejs中下载http网站服务器上托管的文件

[英]How to download a file hosted on http webserver in nodejs

I have created a nodejs http webserver to host some files - 我创建了一个nodejs http webserver来托管一些文件 -

 var http = require('http'),
    fs = require('fs');

var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var qs = require('querystring');
var serve = serveStatic("./");
fs.readFile('./index.html', function (err, html) {
    if (err) {
        throw err; 
    }       
    http.createServer(function(req, res) {  
        var done = finalhandler(req, res);
        serve(req, res, done);
    if(req.method === "POST") {
           if (req.url === "/downloadInstaller") {
              var requestBody = '';
              req.on('data', function(data) {
                requestBody += data;
                if(requestBody.length > 1e7) {
                  res.writeHead(413, 'Request Entity Too Large', {'Content-Type': 'text/html'});
                  res.end('<!doctype html><html><head><title>413</title></head><body>413: Request Entity Too Large</body></html>');
                }
              });
          req.on('end', function() {
              fs1.readFile("./FileToDownload.zip", function(err, data) 
               { res.statusCode = 200;
                 res.setHeader('Content-type', 'text/plain' );
                 res.write(data);
                 return res.end();
              });
          });
        }
    }
    }).listen(8000);
});

Its working good . 它的工作性很好。 I can download a file when I hit url - http://localhost:8000/fileToDownload.extension 我点击url时可以下载文件 - http:// localhost:8000 / fileToDownload.extension

Now , my index.html looks like - 现在,我的index.html看起来像 -

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
<form action="/downloadInstaller" method="post">
    <label>OS Flavor : </Label>
    <input type="text" id="os" name="os"/>
    <input type="submit"/>
</form>

I want to download same file when I will click on submit button.I have written the code for same. 当我点击提交按钮时,我想下载相同的文件。我已经编写了相同的代码。 But it renders the file in browser instead of downloading it. 但它在浏览器中呈现文件而不是下载它。 How Can i achieve it in nodejs? 我如何在nodejs中实现它? Considerably new in nodejs. nodejs中相当新的。

Thanks 谢谢

You should remove this : 你应该删除这个:

 res.setHeader('Content-type', 'text/plain' );

And replace it with headers hinting the browser that it should download the file: 并用提示浏览器应该下载文件的标题替换它:

res.setHeader('Content-Description', 'File Transfer');
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Type', 'application/force-download'); // only if really needed
res.setHeader('Content-Disposition', 'attachment; filename=FileToDownload.zip');

NB: the "force-download" header is a dirty hack, try without it first. 注意:“强制下载”标题是一个肮脏的黑客,首先尝试没有它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM