首页 > 其他分享 >uni-app img header 请求图片

uni-app img header 请求图片

时间:2022-11-16 12:44:58浏览次数:46  
标签:const img xhr res app header blob reader uni

由于 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')
}

 

标签:const,img,xhr,res,app,header,blob,reader,uni
From: https://www.cnblogs.com/CyLee/p/16895492.html

相关文章

  • One or more Filters failed to start. Full details will be found in the appropria
    classpatch下创建logging.properties文件,查看更详细的日志信息handlers=org.apache.juli.FileHandler,java.util.logging.ConsoleHandler######################......
  • es修改mapping
    1.备份数据POST_reindex?wait_for_completion=false&scroll=10m{"source":{"index":"数据索引","size":10000},"dest":{......
  • ASP .NET Core App.Metrics+InfluxDB+Grafana性能监控
    Grafana介绍及部署请参考这篇博客InfluxDB官网GitHubInfluxDB介绍InfluxDB是用Go语言编写的一个开源分布式时序、事件和指标数据库,无需外部依赖InfluxDB在DB-Engi......
  • uni-app背景图片全屏
    uni-app背景图片全屏最初设置好的背景图片总是显示不出来,后来发现最外层view要设置高度才可以使图片正确显示,可设height为100vh。mark一下......
  • Apply Patch 22191577 latest GI PSU to RAC and DB homes using Opatch auto or manu
    Patch22191577:GRIDINFRASTRUCTUREPATCHSETUPDATE11.2.0.4.160119(JAN2016)Unzipthepatch22191577UnziplatestOpatchVersioninoraclehomeandcrshom......
  • 快速搭建自己的Web APP
    如何快速搭建自己WebApp?我推荐今天发现的好东西:Streamlit。这个工具几乎可以无脑创建自己想要的数据交互app,不需要前后端的细节。具体的教程我推荐王老师的如何用Pyth......
  • 不用app实现shopify跳转亚马逊
    shopify跳转亚马逊Amazon的方式有很多种,如果不用app实现shopifyredirecttoAmazon呢?安装app一般都会带有附加文件从而降低网站加载速度,而速度是用户体验/转化率很重要的......
  • 便利店APP/小程序开发
    近年来,线下便利店也在努力寻求转型升级的机会,而APP/小程序开发就是更好的选择,商家可以在线销售便利店的产品。便利店APP/小程序开发都具备哪些功能?1、商品展示功能:......
  • APICloud APP开发指南
    移动端开发大致分为以下四个级别,下面分别介绍了每个级别需要了解和掌握的内容:初级熟悉HTML/CSS/JS前端技术,了解APICloud开发,能使用开发工具运行和预览模板项目,能使......
  • 开发一款共享打印机APP有市场前景吗
    目前的智能硬件设备不论是在种类上还是在功能性方面,都是处于一个不断升级的过程,在打印机上的设备完善同样如此,结合共享打印机APP软件开发的方式,用户能够更好地体会到智能设......