downloadFile(f payload, callback ], call ) f
const { fileName,fileId ] = payload
const index = fileName.indexOf('.')
const result = fileName.substring(index + 1, fileName.length)
let blobType = null
switch(result)
case 'jpg':
blobType='image/jpeg'
break
case 'pdf':
blobType='application/pdf'
break
case "rar':
blobType='application/x-rar-compressed'
break
case 'zip':
blobType='application/zip"
break
case 'doc':
blobType='application/msword
break
case "docx':
blobType='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
break
default:
break
const response = yield call(downloadFile, {fileId, fileName, blobType })
if (response.code === "200') {
if (callback) callback(response.data
}
else if{
message.warn(response.msg 下载失败!')
}
}
export async function downloadFile({fileId,fileName,blobType}){
return request( `${host.fileUrl}/fileDownload/${filed}`,{
method:`POST`,
body: (fileId}
responseType:'blob'
customBlob: true
transferFileName:fileName,
blobType:blobType
})
}
return fetch(giankunPrefix + url+ getparams ,newOptions).then(checkstatus).then((response) => {
const type = response.headers.get('Content-Type')
if ((type && type.tolowerCase().indexOf('application/octet-stream') > -1) || (type && type.toLowerCase().indexOf('bin') > -1 || newOptions.customBlob){
//后台返回的是文件流
下载的文件名也可取响应头的文件名
// const ContentDisposition = response.headers.get('Content-disposition')
// const filenameRegex = /filename[^;=n]*=((['"]).*?\2|[^; \n]*)
// let respFileName = ''
//if (ContentDisposition) {
//const matches = filenameRegex.exec(ContentDisposition)
//if (matches != null && matches[1]) [
//respFileName = decodeURI(matches[1].replace(/[ "]/g,))
//}
//}
response.blob().then((res)=> {
console.log('fetch-blobType',newOptions.blobType)
const blob = new Blob([res],[ type: newOptions.blobType})
const link = document.createElement('a') // 创建a标签
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
//根据解析后的blob对象创建URL 对象
link.download = newOptions.transferFileName // 修改文件名document.body.appendChild(link)
link.click() // 执行下载
document.body.removeChild(link)//下载完成移除标签元素
URL.revokeObjectURL(link.href) // 释放掉blob对象
}).catch((e)=>{
message.info('网络异常')
}
直接打开流文件的方式
const binaryData = [];
binaryData.push(res);
let ur] = window.URL.createObjectURL(res);
window.open(url,"_blank");
return