Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法在浏览器上下载excel文件_Javascript_Node.js_Exceljs - Fatal编程技术网

Javascript 无法在浏览器上下载excel文件

Javascript 无法在浏览器上下载excel文件,javascript,node.js,exceljs,Javascript,Node.js,Exceljs,我正在使用exceljs创建excel文件,并尝试在浏览器上下载相同的文件。但是损坏的文件正在下载 此外,当我尝试使用POSTMAN访问端点时,该文件正在按预期下载 我确实试着看了看照片,但没有多大帮助 //Backend Code: //My route file code, wherein workbook = new Excel.Workbook(); var fileName = 'FileName.xlsx'; res.s

我正在使用exceljs创建excel文件,并尝试在浏览器上下载相同的文件。但是损坏的文件正在下载

此外,当我尝试使用POSTMAN访问端点时,该文件正在按预期下载

我确实试着看了看照片,但没有多大帮助

        //Backend Code:
        //My route file code, wherein workbook = new Excel.Workbook();
        var fileName = 'FileName.xlsx';
        res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        res.setHeader('Content-Disposition', 'attachment; filename=' + fileName);

        return workbook.xlsx.write(res).then(function(){
           res.end();
        });


        //Client Side Code:

        $.ajax({
                    url: "/*****/download",
                    type: "GET",
                    data: body,
                    headers: {
                        'Content-type': 'application/json'
                    },
                    responseType: 'blob',
                    success: function (data) {
                        //Data contains the fields that I want to download
                        var saving = document.createElement('a');
                        var csvData;
//Using a FileSaver or Download library to download the file.
                            saveAs(new Blob([data], { type: "vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8" }), 'test.xlsx');
                        document.body.appendChild(saving);
                        saving.click();
                        document.body.removeChild(saving);
                    },
                    error: function (error) {
                        console.log(error);
                    }
                });

打开excel文件时,它说,我们发现一些内容有问题……是否要恢复……。

您可以使用
URL.createObjectURL

// after getting blob
const tempLink = URL.createObjectURL(blob)
const a = document.createElement('A')
a.download = 'output.xlsx'
a.href = tempLink
a.click()
URL.revokeObjectURL(tempLink) // release memory

首先,为什么要使用
$.ajax
?只需链接到URL@昆汀:服务器上没有文件,我在服务器上动态生成一个文件,并作为流传递到浏览器,我在GET请求中作为主体传递ID。无论如何,删除内容类型没有帮助。“我在GET请求中作为主体传递ID”-你不能这样做,浏览器将从GET请求中删除主体,jQuery中的
数据
属性被添加到查询字符串中,而不是主体。“服务器上没有文件”-这与此无关。浏览器看到的只是一个URL。它不知道或不关心服务器如何生成对请求的响应。“无论如何,删除内容类型没有帮助-我说这是胡说八道,并不是因为它是问题的原因。我也尝试过这一点,但在这种情况下下载的文件也已损坏。
csvData=new Blob([data]),{type:'application/vnd.openxmlformats of icedocument.spreadsheetml.sheet'});var csvUrl=URL.createObjectURL(数据);saving.href=csvUrl;saving.download='123.xlsx';