如何解决如何在fetchUtils react-admin中将responseType设置为blob
我已经尝试使用以下代码将react-admin库中的excel文件(.xlsx)下载到
const httpClient = (url,options = {}) => {
if (!options.headers) {
options.headers = new Headers({
Accept: 'application/json'
});
}
const token = localStorage.getItem('access');
options.headers.set('Authorization',`Bearer ${token}`);
options.headers.set('Accept','application/json,text/plain,*/*');
options.headers.set('Content-Type','application/json; charset=UTF-8');
options.type = 'blob'
return fetchUtils.fetchJson(url,options);
};
httpClient(`${url}`,{
method: 'POST',body: JSON.stringify(file_details),}).then((response) => {
var disposition = request.getResponseHeader('content-disposition');
var matches = /"([^"]*)"/.exec(disposition);
var filename = (matches != null && matches[1] ? matches[1] : 'file.xlsx');
// The actual download
var blob = new Blob([request.response],{
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}).catch(error => {
if (error.body)
notify(`Error occurred: ${error.body.message}`);
else
notify(`Error occurred from server: something went wrong!!`);
});
结果:无法打开损坏的文件。
我已经尝试过将XMLHttpRequest与responseType作为blob一起使用,它的工作原理很酷,但是如果可以通过react-admin实现,请多多关照。 预先谢谢你
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。