• uniapp img header 请求图片


    由于 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')
    }
  • 相关阅读:
    springboot中filter的配置和顺序执行
    springboot整合fastdfs实现上传和下载
    移动端通过fiddler代理调试PC端代码
    react-native windows 环境搭建
    带你逐行阅读redux源码
    前端单页面拆分多个单页面
    Koa2学习(九)与mongoDB交互
    Koa2学习(八)使用session
    Koa2学习(七)使用cookie
    Koa2学习(六)使用koa-router
  • 原文地址:https://www.cnblogs.com/CyLee/p/16895492.html
Copyright © 2020-2023  润新知