router.use("/download",function (req, res, next) {
res.download('d:/myFile/','模板.xlsx',function (err) {
if(err){
res.send(err);
}else{
res.send(true);
}
});
});
后来又返回了以下这个
code:"ENOENT"
errno:-4058
expose:false
path:"E:企业版开通模板.xlsx"
status:404
statusCode:404
syscall:"stat"
res.download()的status code 是200 ok,但返回值都是乱码,这又该咋办呢?
迷茫2017-04-17 15:55:09
Is there something wrong with your path? The specific return value depends on how the interface defines {"code":"EISDIR"}?
Express is a program that runs on the server. The specific directory to be downloaded is determined by the client. For example, the browser settings download to the desktop by default.
The API ofres.download is as follows:
res.download(path, [filename], [fn])
Transfer the file at path as an “attachment”, typically browsers will prompt the user for download. The Content-Disposition “filename=" parameter, aka the one that will appear in the browser dialog is set to path by default, however you may provide an override filename.
When an error has ocurred or transfer is complete the optional callback fn is invoked. This method uses res.sendfile() to transfer the file.
res.download('/report-12345.pdf');
res.download('/report-12345.pdf', 'report.pdf');
res.download('/report-12345.pdf', 'report.pdf', function(err){
if (err) {
// handle error, keep in mind the response may be partially-sent
// so check res.headerSent
} else {
// decrement a download credit etc
}
});