无法使用jquery从chrome浏览器下载.xls文件

Unable to download the .xls file from chrome browser using jquery

本文关键字:浏览器 下载 xls 文件 chrome jquery      更新时间:2023-09-26

可以使用下面的脚本代码在Firefox浏览器中下载。xls文件。

window.open(/api/v1/test/id);

能够下载pdf,图像扩展如jpg,jpeg等。使用上述脚本无法在chrome浏览器中下载。xls文件。

请帮忙解决这个问题

确保在服务器端正确设置Content-Type

例如,在Node For Excel 2007及以上版本中。xlsx文件

response.writeHead(200, {"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});

Chrome 可能将您的内容解释为application/zip

response.writeHead(200, {"Content-Type": "application/zip"});

下面是一个xlsx在Chrome浏览器中被服务时访问localhost:1215

  var http = require("http");
  fileSystem = require('fs'),
  path = require('path');
  var server = http.createServer(function(request, response) {
  var filePath = path.join(__dirname, '1775IronHorse.xlsx');
  var stat = fileSystem.statSync(filePath);
  response.writeHead(200, { "Content-Type": "application/vnd.openxmlformats-    officedocument.spreadsheetml.sheet",
                            'Content-Length': stat.size
                            });
  var readStream = fileSystem.createReadStream(filePath);
  readStream.pipe(response);
});
server.listen(1215);
console.log("Server is listening");

文件以xlsx格式正确下载。

当我删除Content-Type头文件被Chrome解释为zip文件。因此,解决方案是添加Content-Type报头。