最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Express js: How to download a file using POST request - Stack Overflow

programmeradmin3浏览0评论

When I use GET, everything works fine. However, I struggle to use POST to achieve the same effect. Here are the code I have tried:

1.

app.post("/download", function (req, res) {
    res.download("./path");
});

2.

app.post("/download", function (req, res) {
    res.attachment("./path");
    res.send("ok");
});

3.

app.post("/download", function (req, res) {
    res.sendFile("./path");
});

None of them work. What is the correct way to do this?

EDIT: I submit a POST request through a HTML form to /download. ./path is a static file. When I use code in method 1, I can see the correct response header and response body in the developer tool. But the browser does not prompt a download.

When I use GET, everything works fine. However, I struggle to use POST to achieve the same effect. Here are the code I have tried:

1.

app.post("/download", function (req, res) {
    res.download("./path");
});

2.

app.post("/download", function (req, res) {
    res.attachment("./path");
    res.send("ok");
});

3.

app.post("/download", function (req, res) {
    res.sendFile("./path");
});

None of them work. What is the correct way to do this?

EDIT: I submit a POST request through a HTML form to /download. ./path is a static file. When I use code in method 1, I can see the correct response header and response body in the developer tool. But the browser does not prompt a download.

Share Improve this question edited Apr 20, 2015 at 9:32 wdetac asked Apr 20, 2015 at 9:00 wdetacwdetac 2,8325 gold badges21 silver badges44 bronze badges 6
  • which version of express js. are you using. res.download() uses res.sendFile() to transfer files. Also res.sendFile() is supported from Express v4.8.0 onwards. – sachin.ph Commented Apr 20, 2015 at 9:09
  • possible duplicate of Download a file from NodeJS Server using Express – Alex Commented Apr 20, 2015 at 9:10
  • @Pinal That question seems not about POST method as I mentioned. – wdetac Commented Apr 20, 2015 at 9:16
  • @hitman4890 I am using node v0.10.32 and express v4.12.3 – wdetac Commented Apr 20, 2015 at 9:21
  • can you show the calling method or where the request to download is generated code? Also is path is correct? – sachin.ph Commented Apr 20, 2015 at 9:23
 |  Show 1 more comment

1 Answer 1

Reset to default 18

This might not be exactly what you want, but I have been having the same trouble. This is what I did in the end:

  • Client - See EDIT below for updated client code
$http.post('/download', /**your data**/ ).
  success(function(data, status, headers, config) {
    $window.open('/download'); //does the download
  }).
  error(function(data, status, headers, config) {
    console.log('ERROR: could not download file');
  });
  • Server
// Receive data from the client to write to a file
app.post("/download", function (req, res) {
    // Do whatever with the data
    // Write it to a file etc...
});

// Return the generated file for download
app.get("/download", function (req, res) {
    // Resolve the file path etc... 
    res.download("./path");
});

Alternatively, have you just tried calling $window.open(/download); from the HTML? This was the main reason why my download did not start. It returned in the XHR and I could see the data, but also did not prompt a download.

*EDIT: The client code was not accurate, after some more testing it turned out that I only needed to do the following on the client:

// NOTE: Ensure that the data to be downloaded has 
// already been packaged/created and is available
$window.open('/download'); //does the download
发布评论

评论列表(0)

  1. 暂无评论