由于 uni-app 没有 Blob 对象。只能用内置的 uni.download 下载图片了。
wx.downloadFile({ url: `http://192.168.1.1:8999/view/1MsDmdpGRI27FaRDKNmgQ/wx`, header: { 'Device-Info': getDeviceInfo(), 'content-type': 'application/json', Authorization: uni.getStorageSync('token') || '', token: uni.getStorageSync('token') || '' }, responseType: 'arraybuffer', success: (res) => { console.log('downloadfile', res) this.src = res.tempFilePath }, fail(err) { console.log('downloadfile err', err) uni.showToast({ title: '图片下载失败', icon: 'error' }) } })
如果是普通网页的话,我猜测下面的代码片段能带来一些灵感。但具体没实验
// 切记请求头加入 { responseType: 'blob', responseData: 'response',} export function downloadFile(res, name) { const blob = new Blob([res], { type: res.type + ';charset=utf-8', endings: 'transparent' }) console.log(20221116123327, URL.createObjectURL(blob)) }
// 虽然是下载 excel 的,但应该能用到 getExport() { let that = this that.listLoading = true let apiUrl = /api/ResourceCatalog/DataExcel?dbname=${this.params.dbname}&table=${this.params.table} var xhr = new XMLHttpRequest() xhr.open('GET', apiUrl, true) xhr.setRequestHeader('Authorization', 'Bearer ' + this.$store.getters.token) xhr.responseType = 'blob' xhr.onload = function (e) { that.listLoading = false if (this.status === 200) { var blob = new Blob([this.response], { type: 'application/octet-stream', }) var filename = '导出.xls' var url = URL.createObjectURL(blob) var a = document.createElement('a') a.href = url a.download = filename a.click() window.URL.revokeObjectURL(url) } else { // 处理 blob 类型的错误信息 // https://www.cnblogs.com/laoli-boke/p/14049549.html if (this.response.type === 'application/json') { const reader = new FileReader() reader.readAsText(this.response, 'utf-8') reader.onload = () => { const { code, msg } = JSON.parse(reader.result) if (msg.includes('65536')) { that.$message('导出超过65535行数据,无法导出') } else { that.$message(msg) } } } } } xhr.onerror = function (e) { that.$message('导出失败' + e.message) } //发送请求 xhr.send() },
// request.js
// axios request 响应拦截器也可以拦截,直接打开的一种,前提是后端返回的是 blob
if (request.responseType === 'blob') { // 处理blob类型的错误响应 const reader = new FileReader() reader.onload = e => { const responseText = JSON.parse((e.target || {}).result || '{}') const blobMsg = getPropLoop(['msg'], responseText) || '无相关数据' Message.closeAll() Message({ message: blobMsg, type: 'error', duration: 5 * 1000, }) } reader.readAsText(data, 'utf-8') }
原文地址:http://www.cnblogs.com/CyLee/p/16895492.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性