设置请求信息,指定返回是blob格式
export const downloadPdf = (filePath: string) => {
return $docQaReq.commonRequest({
url: `/doc/semantic-doc/document/preview?filePath=${filePath}`,
method: 'get',
responseType: 'blob',
headers: {
'Content-Type': 'application/pdf',
},
})
}
将文件流转换为本地blob临时地址用于访问
function getObjectURL (file: File) {
var binaryData = [];
binaryData.push(file);
let url = null;
if ((window as any).createObjectURL !== undefined) {
url = (window as any).createObjectURL(new Blob(binaryData, {type: 'application/pdf'}));
} else if (window.webkitURL !== undefined) {
try {
url = window.webkitURL.createObjectURL(new Blob(binaryData, {type: 'application/pdf'}));
} catch (error) {
}
} else if (window.URL !== undefined) {
try {
url = window.URL.createObjectURL(new Blob(binaryData, {type: 'application/pdf'}));
} catch (error) {
}
}
return url;
}