• uniapp 微信小程序和h5处理文件(pdf)下载+保存到本地+预览功能


    uniapp实现微信小程序下载资源功能和h5有很大的不同,后台需返回blob文件流

    1.微信小程序实现下载资源功能

    步骤1:下载文件

    uni.downloadFile({
    	url:url,//调接口返回url
    	success:(res)=>{
    		uni.hideLoading();
    		if(res.statusCode==200){
    			var tempFilePath = res.tempFilePath;
    			saveFile(tempFilePath);
    		}else{
    			uni.showToast({
    				icon:'none',
    				title:'报告下载失败'
    			})
    		}
    	},
    	fail:err=>{
    		uni.hideLoading();
    		uni.showToast({
    			icon:'none',
    			title:'报告下载失败'
    		})
    		reject(err);
    	}
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    步骤2:保存文件

    saveFile(tempFilePath){//保存到本地
    	//文件保存到本地
    	uni.saveFile({
    		tempFilePath:tempFilePath,//临时路径
    		success:res=>{
    			uni.showToast({
    				icon:'none',	
    				mask:true,
    				title:'文件已保存'+res.savedFilePath,//保存路径
    				duration:3000
    			})
    			setTimeOut(()=>{
    				//打开文档查看
    				uni.openDocument({
    					filePath:res.savedFilePath,
    					success:res=>{
    						console.log('打开文档成功');
    					}
    				})
    			},3000)
    		}
    	})
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    步骤3:打开文档查看

    //打开文档查看
    uni.openDocument({
    	filePath:res.savedFilePath,
    	success:res=>{
    		console.log('打开文档成功');
    	}
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.H5实现下载资源功能

    步骤1:获取下载文件

    uni.downloadFile({
    	url:url,//调接口返回的url
    	success:res=>{
    		uni.hideLoading();
    		if(res.statusCode==200){
    			var tempFilePath = res.tempFilePath;
    			saveFile(tempFilePaht);
    		}else{
    			uni.showToast({
    				icon:'none',
    				title:'报告下载失败'
    			})
    		}
    	},
    	fail:err=>{
    		uni.hideLoading();
    		uni.showToast({
    			icon:'none',
    			title:'报告下载失败'
    		})
    		reject(err);
    	}
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    步骤2:保存文件

    saveFile(url){//保存到本地
    	try{
    		const fileName = '报告名称';
    		//new Blob 实例化文件流
    		//let url = fileData
    		//const url = window.URL.createObjectURL(new Blob([fileData],{type:'application/pdf'}))
    		const link = document.createElement('a');
    		link.style.display = 'none';
    		link.href = url;
    		link.setAttribute('download',fileName);
    		document.body.appendChild(link);
    		link.click();
    		//下载完成移除元素
    		document.body.removeChild(link);
    		//释放掉blob对象
    		window.URL.revokeObjectURL(url)
    		uni.showToast({
    			title:'下载成功'
    		})
    	}catch(error){
    		uni.showToast({
    			title:'下载失败'
    		})
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    h5浏览文件直接后台给文件地址即可。

    以上内容摘自https://blog.csdn.net/panfu163/article/details/132832484

    以上代码并未通过验证,后续使用到,有问题的话会补充。

  • 相关阅读:
    gcc编译器
    servlet的生命周期
    垃圾回收机制的算法实现——增量式垃圾回收算法
    Java基于基于微信小程序的快递柜管理系统
    tiny4412编译与移植uboot
    网络分析笔记05:解析TCP/IP的链路层
    钡铼技术R40路由器应用于农业大数据采集与分析系统
    AJAX(一):初识AJAX、http协议、配置环境、发送AJAX请求、请求时的问题
    ACL2021论文笔记(3篇)
    Spring 事务(测试)--在这个笔记中记录的是没有添加事务,数据库返回的效果。...
  • 原文地址:https://blog.csdn.net/yehaocheng520/article/details/136650118