javascript - 如何将文件(exe 或 rar )提供给客户端以从 node.js 服务器下载?

标签 javascript node.js

我有一个 node.js 服务器,它提供一个带有密码文本输入的 index.html。 在检查服务器端密码后,应该开始为客户端下载。 客户端不应该能够看到文件在服务器上的位置路径。

这是我的 server.js:

var
    http = require('http'),
    qs = require('querystring'),
        fs = require('fs') ;
console.log('server started');

var host = process.env.VCAP_APP_HOST || "127.0.0.1";
var port = process.env.VCAP_APP_PORT || 1337;

http.createServer(function (req, res) {

    if(req.method=='GET') {

        console.log ( ' login request from    ' + req.connection.remoteAddress );



            fs.readFile(__dirname +'/index.html', function(error, content) {
                if (error) {
                    res.writeHead(500);
                    res.end();
                }
                else {
                    res.writeHead(200, { 'Content-Type': 'text/html' });
                    res.end(content, 'utf-8');
                }
            });


    }  // method GET  end

    else{ // method POST start


        console.log('POST request from   ' + req.connection.remoteAddress);
        var body = '';
        req.on('data', function (data) {
            body += data;

            if (body.length > 500) {
                // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST
                req.connection.destroy(); console.log('too much data')}
        });

        req.on('end', function () {

            var postdata = qs.parse(body);
            var password = postdata.passwordpost  ;


      if (password == '7777777') {
               console.log('the password is right, download starting');

             // ???????????????????????????????????                         here I need help from stackoverflow



      }


          else{
          console.log ('password wrong');
          fs.readFile(__dirname +'/wrongpassword.html', function(error, content) {
              if (error) {
                  res.writeHead(500);
                  res.end();
              }
              else {
                  res.writeHead(200, { 'Content-Type': 'text/html' });
                  res.end(content, 'utf-8');
              }
          });
      }
        });       // req on end function end

    }
}).listen(port, host);

我需要帮助的部分标有????????

这是我的 index.html:

<html>
<body>
<br>  <br>
&nbsp;&nbsp;&nbsp; please enter your password to start your download
<br>  <br>

<form method="post" action="http://localhost:1337">
    &nbsp;&nbsp;&nbsp;
    <input type="text" name="passwordpost" size="50"><br><br>
    &nbsp;&nbsp;&nbsp;   &nbsp;&nbsp;&nbsp; &nbsp;
    <input type="submit" value="download" />
</form>

</body>
</html>

你知道怎么做吗?

最佳答案

当然,您可以在代码中使用它:

res.setHeader('Content-disposition', 'attachment; filename='+filename);
//filename is the name which client will see. Don't put full path here.

res.setHeader('Content-type', 'application/x-msdownload');      //for exe file
res.setHeader('Content-type', 'application/x-rar-compressed');  //for rar file

var file = fs.createReadStream(filepath);
//replace filepath with path of file to send
file.pipe(res);
//send file

关于javascript - 如何将文件(exe 或 rar )提供给客户端以从 node.js 服务器下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16500615/

相关文章:

javascript - HTML5 Canvas 覆盖在网页之上?

javascript - 表单验证失败但仍然提交

node.js - sinon.js - 如何组织我的 stub ?

javascript - 如何跳过或跳转中间件? ( Node )

node.js - 为什么这个可调用的云函数失败并返回 "app":"MISSING"?

Node.js 传出 Http 请求连接限制(不能建立超过五个的连接)

JavaScript 在 window.onerror 中处理所有错误

javascript - 为什么 'getTimezoneOffset' 在 Date.prototype 中实现而不是作为 Date 的静态方法?

javascript - 如何从数组中删除重复的点?

javascript - Child_process 抛出错误 : write EPIPE