英文:
Export to .xls works on dev environment but not on production / Failed - Network Error
问题
以下是您提供的英文内容的中文翻译:
我似乎找不到我的代码问题,它在我的开发环境中运行得很完美,但在生产环境中不起作用。这不是浏览器的问题(Firefox、IE和Chrome都失败了),文件大小也不是问题,因为其他问题在这里都没有。我以为可能是令牌的问题,因为一开始我没有提供令牌,但在实施后,我发现问题仍然存在。
当要求下载时,文件显示“失败 - 网络错误”。
以下是一些代码片段,从客户端开始:
generateExcel() {
this.detector.markForCheck();
this.api.post('invoice/excel_liquidation', { liquidation: this.list }).subscribe((response: any) => {
if (response.success) {
this.toast.success('Excel生成成功!');
let url = response.file.url // + '?token=' + this.auth.jwt;
this.generateDownloadUrl(url, response.file.name);
this.detector.markForCheck();
}
this.detector.markForCheck();
}, (error: any) => {
this.detector.markForCheck();
});
}
generateDownloadUrl(url: string, name: string) {
let a = document.createElement('a');
a.href = url;
a.download = name;
document.body.appendChild(a);
a.click();
a.remove();
}
最后是服务器端的代码(除了解析和其他不必要的数据):
exportExcel: (headers, content, file_name) => {
/* 一些错误处理 */
if (!file_name) file_name = 'export-' + app.date.now('YYYYMMDDHHmmss');
let file = '';
headers.map(header => {
if (typeof header === 'string' && header.toString().trim() !== '') header = header.toString().trim().replace(/_/g, ' ').toUpperCase();
file += header + '\t';
});
file += '\n';
content.map(row => {
headers.map(header => {
file += row[header] + '\t';
});
file += '\n';
});
let save_file = app.paths.storage + 'temp/' + file_name + '.xls';
return fs.writeFileAsync(save_file, file, 'utf-8').then(() => {
return Promise.resolve({ path: save_file });
});
},
任何帮助都将不胜感激。
英文:
I can't seem to find issues with my code, and it is working perfectly in my dev environment, but not on production. This is not an issue with the browser (Firefox, IE, and Chrome all three fail), and neither is it an issue with the size of the file, as other questions here indicate. I thought that it could be a Token problem, since I didn't provide one at first, but after implementing it I find the problem is still present.
The file shows Failed - Network Error
when prompted to download.
Here are some code bits, starting from client-side
generateExcel( ){
this.detector.markForCheck( );
this.api.post( 'invoice/excel_liquidation', { liquidation: this.list } ).subscribe( ( response: any ) => {
if( response.success ) {
this.toast.success( 'Excel correctly generated!' );
let url = response.file.url // + '?token=' + this.auth.jwt;
this.generateDownloadUrl( url, response.file.name );
this.detector.markForCheck( );
}
this.detector.markForCheck( );
}, ( error: any ) => {
this.detector.markForCheck( );
});
}
generateDownloadUrl( url: string, name: string ) {
let a = document.createElement( 'a' );
a.href = url;
a.download = name;
document.body.appendChild( a );
a.click( );
a.remove( );
}
And finally the server-side (except parsing, and other unnecessary data):
exportExcel: ( headers, content, file_name ) => {
/* SOME ERROR HANDLING */
if( !file_name ) file_name = 'export-' + app.date.now( 'YYYYMMDDHHmmss' );
let file = '';
headers.map( header => {
if( typeof header === 'string' && header.toString( ).trim( ) !== '' ) header = header.toString( ).trim( ).replace( /_/g, ' ' ).toUpperCase( );
file += header + '\t';
});
file += '\n';
content.map( row => {
headers.map( header => {
file += row[ header ] + '\t';
});
file += '\n';
});
let save_file = app.paths.storage + 'temp/' + file_name + '.xls';
return fs.writeFileAsync( save_file, file, 'utf-8' ).then( ( ) => {
return Promise.resolve( { path: save_file } );
});
},
Any help is much appreciated.
答案1
得分: 0
问题在修改之前的方法后得以解决,修改后的方法如下:
generateDownloadUrl(url: string, name: string) {
let a = document.createElement('a');
a.href = url;
/*a.download = name;*/
a.target = '_blank';
document.body.appendChild(a);
a.click();
a.remove();
}
简而言之,尝试在新标签页中打开.xls文件,而不是直接下载它,作为解决该问题的一种变通方法。
英文:
The issue was solved after modifying the previous method like this:
generateDownloadUrl( url: string, name: string ) {
let a = document.createElement( 'a' );
a.href = url;
/*a.download = name;*/
a.target = '_blank';
document.body.appendChild( a );
a.click( );
a.remove( );
}
In short, trying to open the .xls file in a new tab instead of directly downloading it served as a workaround for the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论