在uniapp中开发微信小步伐中下载文件
需要使用的api:downloadFile,previewImage,openDocument,如果是图片我们就使用previewImage预览,预览的同时也可以下载到当地,其他文件不能预览的使用openDocument打开,打开后可以保存得手机当地。
预览效果
点击图片预览效果:
点击非图片类型时直接当地打开
代码:
- openFile(url) {
- const isImgType = ['jpg', 'png', 'bmp', 'jpeg', 'webp']
- uni.showLoading({ title: '加载中...' })
- uni.downloadFile({
- url,
- success: (res) => {
- const fileType = res.tempFilePath.split('.').pop()
- if (isImgType.includes(fileType)) {
- uni.previewImage({ // 调用微信api预览图片
- showmenu: true, // 开启时右上角会有三点,点击可以保存
- urls: [res.tempFilePath],
- current: res.tempFilePath,
- success: (res) => {
- uni.hideLoading()
- console.log('打开图片成功')
- },
- fail: (res) => {
- console.log(res)
- console.log('打开图片失败')
- },
- })
- } else {
- uni.openDocument({
- filePath: res.tempFilePath,
- showMenu: true, // 开启时右上角会有三点,点击可以保存
- success: (res) => {
- uni.hideLoading()
- console.log('打开文档成功')
- },
- fail: (res) => {
- console.log(res)
- console.log('打开文档失败')
- },
- })
- }
- },
- fail: (e) => {
- console.log(e)
- },
- })
- },
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |