通过aurelia http客户端下载Excel文件
我正在进行一项任务,其中我必须下载xlsx格式的报告。报告文件已从服务器成功生成,并在客户端(aurelia http客户端)接收,但我不知道如何进一步下载。我将在回答中执行类似操作 。。。最终会在奥雷利亚发生类似这样的事情:通过aurelia http客户端下载Excel文件,aurelia,Aurelia,我正在进行一项任务,其中我必须下载xlsx格式的报告。报告文件已从服务器成功生成,并在客户端(aurelia http客户端)接收,但我不知道如何进一步下载。我将在回答中执行类似操作 。。。最终会在奥雷利亚发生类似这样的事情: .withResponseType('blob') .withInterceptor({ response(message) { var defaultFileName = "defa
.withResponseType('blob')
.withInterceptor({
response(message) {
var defaultFileName = "default.txt";
var disposition = message.headers.headers['content-disposition']?message.headers.headers['content-disposition']:message.headers.headers['Content-Disposition'];
if (disposition) {
var match = disposition.match(/.*filename=\"?([^;\"]+)\"?.*/);
if (match[1])
defaultFileName = match[1];
}
defaultFileName = defaultFileName.replace(/[<>:"\/\\|?*]+/g, '_');
if (navigator.msSaveBlob)
return navigator.msSaveBlob(message.response, defaultFileName);
var blobUrl = window.URL.createObjectURL(message.response);
var anchor = document.createElement('a');
anchor.download = defaultFileName;
anchor.href = blobUrl;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
}
})
.withResponseType('blob'))
.带拦截器({
回应(讯息){
var defaultFileName=“default.txt”;
var disposition=message.headers.headers['content-disposition']?message.headers.headers['content-disposition']:message.headers.headers['content-disposition'];
如果(处置){
var match=disposition.match(/.*filename=\”?([^;\“]+)\”?*/);
if(匹配[1])
defaultFileName=匹配[1];
}
defaultFileName=defaultFileName.replace(/[:“\/\\\\\\\\\?*]”+/g,“\”);
if(navigator.msSaveBlob)
返回navigator.msSaveBlob(message.response,defaultFileName);
var blobUrl=window.URL.createObjectURL(message.response);
var anchor=document.createElement('a');
anchor.download=defaultFileName;
anchor.href=blobUrl;
document.body.appendChild(锚定);
anchor.click();
document.body.removeChild(锚定);
}
})
Helo with.withInterceptor()在响应中生成错误,将其更改为在无响应中修复错误,并同时卸载多个文件
getLogsCsv(param) {
this.http.configure(config => {
config
.withResponseType('blob');
});
return this.http.get("/admin/api/logs" + param)
.then(response => {
if (response.statusCode == 200) {
var defaultFileName = "FileName.csv";
var blobUrl = window.URL.createObjectURL(response.response);
var anchor = document.createElement('a');
anchor.download = defaultFileName;
anchor.href = blobUrl;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
return response.content;
} else {
console.log('response was not ok.');
console.log(response);
}
})
.catch(error => {
console.log(error);
});
}
我用过图书馆。安装库,将其添加到aurelia.json
中,然后添加
import * as download from 'downloadjs'
然后按如下方式编写代码:
this.httpClient.fetch('your/url/here')
.then((response: Response) => response.blob())
.then((blob: Blob) => download(blob, 'filename.extension', 'mime type of the file'));
瞧,你的文件将被下载