【发布时间】:2022-10-07 23:07:48
【问题描述】:
使用 fetch 我可以通过执行以下代码来创建下载链接
const result = await fetch(authLinkProps.url, {
headers: {
Authorization: getUserToken(),
},
});
const blob = await result.blob();
const href = window.URL.createObjectURL(blob);
一切正常,但我在我的项目中使用 React Query,所以我希望保持一致并使用它来做同样的事情。
如果我执行以下操作
const { data, error } = useDocuments(authLinkProps.url);
let href: string = '';
console.log(data);
if (data !== undefined) {
href = window.URL.createObjectURL(data);
}
useDocuments 在哪里
return useQuery<Blob, Error>('document-download', async () => {
const dwnldHeader = {
headers: {
Authorization: currentUser,
responseType: 'blob',
},
};
const { data } = await axios.get<Blob>(apiUrl, dwnldHeader);
return data;
});
我得到错误
"Uncaught (in promise) TypeError: Failed to execute 'createObjectURL' on 'URL': Overload resolution failed."
如果我将其控制出来,数据看起来像一团?
有任何想法吗?
【问题讨论】:
标签: reactjs download react-query