5

パスワードのテキスト入力を含むindex.htmlを提供するnode.jsサーバーがあります。サーバー側のパスワードを確認した後、クライアントのダウンロードが開始されます。クライアントは、ファイルがサーバー上にある場所のパスを確認できないはずです。

ここに私の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>

これを行う方法を知っていますか?

4

4 に答える 4

5

もちろん、コードでこれを使用できます:

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
于 2013-05-11T20:38:58.923 に答える
4

You needs to declare and require the path: path = require("path")

then can do:

var uri = url.parse(request.url).pathname
    , filename = path.join(process.cwd(), uri);

path.exists(filename, function(exists) {
    if(!exists) {
        response.writeHead(404, {"Content-Type": "text/plain"});
        response.write("404 Not Found\n");
        response.end();
        return;
    }
response.writeHead(200);
response.write(file, "binary");
response.end();
}

check these complete example.

于 2013-05-11T19:10:33.620 に答える
2

Express Web フレームワークを使用する場合は、はるかに簡単な方法で実行できます。

app.get('/download', function(req, res){
  var file = __dirname + 'learn_express.mp4';
  res.download(file); // Sets disposition, content-type etc. and sends it
});

高速ダウンロード API

于 2014-09-10T08:24:23.400 に答える