欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > 【uniapp 解决h5 uni.saveFile 不生效】2种方法解决

【uniapp 解决h5 uni.saveFile 不生效】2种方法解决

2024/10/25 5:15:49 来源:https://blog.csdn.net/weixin_49066399/article/details/141710267  浏览:    关键词:【uniapp 解决h5 uni.saveFile 不生效】2种方法解决

用uni.saveFile h5报错 saveFile API saveFile is not yet implemented
查看文档发现不支持h5
在这里插入图片描述
解决方法:
这个我用了pc 端一样的方法用a标签来下载保存代码如下:
第一种:

 const a = document.createElement('a');a.href = filePath; //filePath 这里的地址是 uni.downloadFile 中的返回值里的地址
a.download = finName; // 可以设置下载文件名,如果是空字符串,则使用服务器端设置的文件名
a.click();

第二种 可以引用js第三方库来进行保存
先安装 file-saver

npm install file-saver --save

引用

	import { saveAs } from 'file-saver';//filePath 这里的地址是 uni.downloadFile 中的返回值里的地址//finName 下载文件名saveAs(filePath,finName)

整体代码:

fileDwonload(url='https://example.com/somefile.pdf') {uni.showLoading({title: '下载中'});const index = url.lastIndexOf('/');const finName = url.slice(index + 1, url.length)//下载文件uni.downloadFile({ url: url, //请求地址savePath: finName,success: (res) => {//下载成功if (res.statusCode === 200) {//保存文件let filePath = res.tempFilePath;// #ifdef H5 // saveAs(filePath,finName);const a = document.createElement('a');a.href = filePath;a.download = finName; // 可以设置下载文件名,如果是空字符串,则使用服务器端设置的文件名a.click();// #endif//  #ifdef MP-WEIXIN || APP-PLUS 	uni.saveFile({tempFilePath: filePath,success: function(filRes) {console.log(filRes, '=====>');},fail: function(err) {console.log('保存失败');}})// #endif			uni.hideLoading()}
},
fail: (e) => {console.log(e, '文件下载失败')uni.showToast({title: '文件下载失败',icon: "error",})}
});}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com