上传下载

This commit is contained in:
caoyan
2026-09-03 13:53:54 +08:00
parent 63dc3bcc89
commit f0da970904
18 changed files with 233 additions and 447 deletions
+43
View File
@@ -183,4 +183,47 @@ export function fileDown (downLoadUrl, fileName, id,data) {
// console.log("fileDownloadCallback 下载文件 回调 " + JSON.stringify(obj));
// }
/**
* 通过 POST 接口下载图片并返回 blob URL
* @param {string} attach_id 文件ID
* @returns {Promise<string>} blob URL
*/
export async function getImageBlobUrl(attach_id) {
try {
const { downloadFile } = await import('@/api/api')
const blob = await downloadFile(attach_id)
if (blob instanceof Blob && blob.size > 0) {
const type = blob.type && blob.type.startsWith('image/') ? blob.type : 'image/jpeg'
const imgBlob = new Blob([blob], { type })
const url = URL.createObjectURL(imgBlob)
console.log('getImageBlobUrl ok: id=', attach_id, 'type=', blob.type, 'size=', blob.size, 'url=', url)
return url
}
console.error('getImageBlobUrl 无效响应: id=', attach_id, 'isBlob=', blob instanceof Blob, 'size=', blob && blob.size, 'type=', blob && blob.type)
return ''
} catch (e) {
console.error('getImageBlobUrl 请求失败: id=', attach_id, e)
return ''
}
}
/**
* 通过 POST 接口下载文件并触发浏览器保存
* 注意:后端返回的是文件流(Blob),浏览器不会自动下载,需手动触发
* @param {string} attachId 文件ID
* @param {string} [fileName] 保存的文件名(带后缀),缺省为 'download'
*/
export async function downloadFileById(attachId, fileName) {
const { downloadFile } = await import('@/api/api')
const blob = await downloadFile(attachId)
if (!(blob instanceof Blob)) return
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = fileName || 'download'
document.body.appendChild(a)
a.click()
a.remove()
URL.revokeObjectURL(url)
}
export default service