【问题标题】:how to download server file in node js?如何在节点 js 中下载服务器文件?
【发布时间】:2019-01-23 23:27:22
【问题描述】:

你能告诉我如何在节点js中下载服务器文件

这是我的代码 节点js代码(请求代码)

var express = require('express');
var multer = require('multer');
var bodyParser = require('body-parser');
var cors = require('cors');
var app = express();
var path = require('path');
var PORT = process.env.PORT || 3000;


// use of body parser
app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(bodyParser.json());
app.use(cors());



app.get('/download', function(req, res){
    console.log(__dirname);
    var file = path.join(__dirname , '/uploads/file-1534458777206.xlsx');
    console.log(file)
    res.download(file); // Set disposition and send it.
});




app.listen(PORT, () => {
    console.log(`App is listening to ${PORT}`);
})

我在在客户端提出这样的要求

$('.download').on('click', function () {
            $.ajax({
                url: 'http://localhost:3000/download',
                type: 'GET',
                dataType: 'json',
                success: function (data) {
                    console.log('data')
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    // Handle errors here
                    console.log('ERRORS: ' + textStatus);
                    // STOP LOADING SPINNER
                }
            });

在控制台上出现错误

ERRORS: parsererror
3index.html?_ijt=9lu9erpan2oq6qf28851ngj0ra:32 ERRORS: parsererror
2index.html?_ijt=9lu9erpan2oq6qf28851ngj0ra:32 ERRORS: parsererror

服务器日志

C:\Users\B0207296\WebstormProjects\uploadFile\server
C:\Users\B0207296\WebstormProjects\uploadFile\server\uploads\file-1534458777206.xlsx

为什么文件没有在客户端下载,因为我提到文件存在于上面的网址中

【问题讨论】:

  • 点击后会发生什么$window.open(http://localhost:3000/download)
  • 它在新标签中下载文件
  • 使用它下载文件 window.open(localhost:3000/download) .in new tab
  • 好的,您的服务器工作正常。我认为用户浏览器正在保护他们。我不知道有什么方法可以直接从 ajax 请求下载文件。在这里尝试“虚拟点击链接”方法我猜stackoverflow.com/questions/35164277/…
  • 好的,谢谢您的帮助

标签: javascript jquery node.js


【解决方案1】:

通过 ajax 获取文件内容: 只需删除 dataType,您的文件正在被解析为导致错误的 json。

$('.download').on('click', function () {
     $.ajax({
         url: 'http://localhost:3000/download',
          type: 'GET',
          success: function (data) {
              console.log(data); // File data
          },
          error: function (jqXHR, textStatus, errorThrown) {
              // Handle errors here
              console.log('ERRORS: ' + textStatus);
              // STOP LOADING SPINNER
          }
      });

要下载文件最好使用虚拟链接

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>
<body>
<b class="download">Download Here</b>
<script>
$('.download').on('click', function () {
    var link = document.createElement('a');
    link.href = 'http://localhost:3000/download';
    var e = document.createEvent('MouseEvents');
    e.initEvent('click', true, true);
    link.dispatchEvent(e);
  })
</script>

</body>
</html>

【讨论】:

  • 是的 success is 现在来了但是文件没有下载为什么?
  • 使用此window.location ='http://localhost:3000/download'; 正确或成功下载文件
  • 但它没有使用 ajax 下载
  • 在这种情况下,您在 ajax 请求中获得了文件内容。如果你想下载它最好使用虚拟链接点击策略。
猜你喜欢
  • 2019-06-26
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-15
相关资源
最近更新 更多