uniapp实现非图片文件下载(doc,xls.zip等格式)

使用h5+ API实现文件下载,APP和webview均可使用

uni.showLoading()
var name = '承诺书模板.xls';   //文件名称可以在上传时进行保存,下载时取出,当文件名称中存在单双引号时,要做好处理,否则会报错
var url = 'http://web.gdut.edu.cn/~jsxy/doc/keshitongji.xls';   //文件名称可以在上传时进行保存,下载时取出,当文件名称中存在单双引号时,要做好处理,否则会报错,路径大家根据需求替换,这是我自己网上找的demo
var dtask = plus.downloader.createDownload(url,{
	filename:"_downloads/"+name            //利用保存路径,实现下载文件的重命名
},function(d,status){
	uni.hideLoading()
	console.log('d', d, status)
	//d为下载的文件对象
	if(status==200){
		console.log('下载成功')
		//下载成功,d.filename是文件在保存在本地的相对路径,使用下面的API可转为平台绝对路径
		this.localPath = plus.io.convertLocalFileSystemURL(d.filename);
		plus.runtime.openFile(d.filename);	   //选择软件打开文件
    }else{
		console.log('下载失败')
    	//下载失败
    	plus.downloader.clear();        //清除下载任务
    }
})
dtask.start();

使用uniapp的API实现文件下载

var _this = this
uni.downloadFile({
  url: 'http://web.gdut.edu.cn/~jsxy/doc/keshitongji.xls',
  success: function (res) {
		var path = res.tempFilePath
		console.log('res', res);
		if (res.statusCode === 200) {
			// preview
			uni.openDocument({
				filePath: path,
				success: function(res) {
					console.log('打开文档成功');
					console.log(res);
				}
			});
			//文件保存
			uni.saveFile({
				tempFilePath: path,
				success: function(res) {
					_this.localPath = res.savedFilePath;
					console.log(savedFilePath)
					uni.showToast({
						title: '下载成功',
						content: savedFilePath,
						duration: 500
					});
					uni.getSavedFileList({
						success: function(res) {
							console.log(res.fileList);
						}
					});
				}
			});
		}
	},
	fail(err){
		console.log('下载失败', err)
	}
});
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在uni-app中使用`plus.zip.compressVideo`方法压缩视频需要使用HBuilderX进行开发,并在真机环境下运行。 以下是一个示例代码,演示如何在uni-app中使用`plus.zip.compressVideo`方法压缩视频: ```vue <template> <view> <button @click="compressVideo">压缩视频</button> </view> </template> <script> export default { methods: { compressVideo() { const self = this; plus.gallery.pick(function (e) { plus.zip.compressVideo({ src: e, dst: '_doc/compressed.mp4', quality: 0.5, overwrite: true }, function (event) { console.log('视频压缩成功'); console.log('压缩后的视频路径:' + event.target); self.getCompressedVideoSize(event.target); }, function (error) { console.log('视频压缩失败:' + error.message); }); }, function (error) { console.log('选择视频失败:' + error.message); }, { filter: 'video', multiple: false }); }, getCompressedVideoSize(path) { plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function (fs) { fs.root.getFile(path, { create: false }, function (fileEntry) { fileEntry.getMetadata(function (metadata) { // 获取压缩后的视频文件大小 const size = metadata.size; console.log('压缩后的视频文件大小:' + size + ' bytes'); }, function (error) { console.log('获取压缩后的视频文件大小失败:' + error.message); }); }, function (error) { console.log('获取压缩后的视频文件失败:' + error.message); }); }, function (error) { console.log('请求文件系统失败:' + error.message); }); } } } </script> ``` 在这个示例中,我们通过点击按钮调用`compressVideo`方法来选择视频并进行压缩。在视频压缩成功后,我们通过`plus.io.requestFileSystem`来获取压缩后的视频文件大小。 需要注意的是,使用`plus.zip.compressVideo`方法进行视频压缩需要在真机环境下运行,无法在模拟器中测试。 希望这个示例对你有帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值