下载响应发送到通过nodejs POST请求的文件?发送到、文件、nodejs、POST

由网友(挥不去的悲伤)分享简介:我无法发出任何下载文件中的POST命令。I'm having trouble issuing a POST command that downloads a file.在客户端,我想张贴到一个特定的URL,包括参数,指定要下载的文件。On the client side, I'm trying to POST t...

我无法发出任何下载文件中的POST命令。

I'm having trouble issuing a POST command that downloads a file.

在客户端,我想张贴到一个特定的URL,包括参数,指定要下载的文件。

On the client side, I'm trying to POST to a specific URL, including a param that specifies the file to download.

var req = $.ajax({
    type: 'POST',
    url : '/click',
    data: { 'path' : filename }
   });
req.done(function(data) {
// Download the file here?

服务器最终触发了一种方法,做到这一点:

The server eventually fires off a method which does this:

function downloadFile(req, res) {
  var dir = req.session.currentdir + req.body.path;
  mimetype = (shell.exec("file --mime-type '" + dir + "'", {silent:true}).output);
  mimetype = mimetype.substring(mimetype.indexOf(": ") + 2, mimetype.length);

  var stat = fs.statSync(dir);
  res.writeHead(200, {'Content-Type' : mimetype,
                      'Content-Length': stat.size });
  var fileStream = fs.createReadStream(dir);
  fileStream.pipe(res);
};

现在我似乎无法获得客户端接受我试图管道的文件。 。它只是挂了一个令人难以置信的长时间关闭之前。什么是适当的方式来获得客户端下载我想回送文件?

Now I can't seem to get the client side to accept the file I'm trying to pipe back . . it just hangs for an incredibly long time before closing. What is the appropriate way to get the client to download the file I'm trying to send back?

许多感谢您抽出宝贵时间阅读。

Much thanks for taking the time to read.

推荐答案

1。 resp.setHeader(内容处置,附件;文件名= xxxx.xxx );

1. resp.setHeader( "Content-Disposition", "attachment; filename="xxxx.xxx"" );

2。 最好使用GET

2. better to use Get

阅读全文

相关推荐

最新文章