下载响应发送到通过nodejs POST请求的文件? [英] Downloading a file sent in response to a POST request via nodejs?

查看:460
本文介绍了下载响应发送到通过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 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

这篇关于下载响应发送到通过nodejs POST请求的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆