uniapp预览文件(后端返回来的是工作流)

// 这个方法是公用的可以写在mixin.js里面
import download from '@/utils/dowload'
// 下载
		_download(url,id,name){
			download({
			  url: `${url}/${id}`,
			  method: 'get'
			}).then((res)=>{
				wx.getFileSystemManager().saveFile({
					tempFilePath: res, 
					success: function(res) {
						var filePath = res.savedFilePath;
						uni.showModal({
							title: "提示",
							content: "文件已保存:" + name?name:filePath ,
							cancelText: "关闭",
							confirmText: "打开文件",
							success: (res2) => {
								if (res2.confirm) {
									uni.openDocument({
										filePath: filePath,
										showMenu: true,
									});
								}
								
							}
						})	
					}
				})
			})
		}
// utils/dowload.js 代码

import config from '@/config'
import { getToken } from '@/utils/auth'
import { toast} from '@/utils/common'

let timeout = 10000
const baseUrl = config.baseUrl
let timer = null

const download = config => {
  // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  config.header = config.header || {}
  if (getToken() && !isToken) {
    config.header['Authorization'] = 'Bearer ' + getToken()
  }
	config.header['content-type'] = 'application/json'
	return new Promise((resolve, reject) => {
    uni.downloadFile({
        url: config.baseUrl || baseUrl + config.url,
        header: config.header,
      }).then(response => {
        let [error, res] = response
        if (error) {
          toast('后端接口连接异常')
          reject('后端接口连接异常')
          return
        }
        const code = res.statusCode  || 200
   
        resolve(res.tempFilePath)
      }) .catch(error => {
        let { message } = "下载失败"
        toast(message)
        reject(error)
      })
    
  })
}

export default download

//使用
this._download('/out/download',id,`下载文件${id}`)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值