欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

uni-app使用uni-download和uni.saveFile下載保存文件遇到的問題及解決方法

 更新時(shí)間:2024年01月23日 09:58:40   作者:漁夫李  
這篇文章主要給大家介紹了關(guān)于uni-app使用uni-download和uni.saveFile下載保存文件遇到的問題及解決方法的相關(guān)資料,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下

項(xiàng)目場景:

app里的開發(fā)的小程序需要下載文件功能以及一個(gè)下載中心頁面可以查看所有下載的文件,使用了uni.download下載以及uni.saveFile保存文件

下載中心頁面實(shí)現(xiàn)邏輯

1.下載文件后保存文件名和uni.saveFile返回的路徑uni.setStorageSync到緩存里

uni.downloadFile({
					    method: 'GET',
					    url: 你的url,
					    timeout: 5000,
					   header:{
					     authorization: 你的token,
					   },
					    success: (data) => {
							//接口返回的文件流
							console.log("fileData",data)
					            if (data.statusCode === 200) {
					                    //文件保存到本地
					                    uni.saveFile({
					                            tempFilePath: data.tempFilePath, //臨時(shí)路徑
					                            success: function(res) {
					                                console.log("data.",res)
														let list = uni.getStorageSync("__local_down_history");
														if (list.length) {
															let arrNew=list
															let newObj={
																path:res.savedFilePath,
																name:fundcode+"_"+fundNo+'.'+fileType
															}
															arrNew.unshift(newObj);
															_this.localSearchList=arrNew
															// arrUnique(this.localSearchList);
														} else {
															_this.localSearchList = [{
																path:res.savedFilePath,
																name:fundcode+"_"+fundNo+'.'+fileType
															}];
														}
														console.log("文件緩存",_this.localSearchList)
														uni.setStorageSync("__local_down_history", _this.localSearchList);        
					                            }
					                    });
										
					            }
					    },
					    fail: (err) => {
					            console.log(err);
					            // uni.showToast({
					            //         icon: 'none',
					            //         mask: true,
					            //         title: '失敗請重新下載',
					            // });
					    },
					})

2.下載中心讀取uni.getStorageSync緩存的文件列表

<ourLoading isFullScreen :active='loadingStatus' text="加載中..." color='rgb(0, 106, 254)' textColor='rgb(0, 106, 254)'	 />
<uni-list class="uni-list" :border="false" style="margin-bottom: 50px;">
				<!-- 列表渲染 -->
				<uni-list-item v-for="(item,index) in currnetArr" :key="index" >
					<template v-slot:body>
						<view class="main">
							<view class="mainContent"  >
							//節(jié)流打開文件
									<text class="author" style="color: #89939B;" @tap="$u.throttle(openURL(item.path), 1000)">{{item.name}}</text>
							</view>
								
						</view>
					</template>
				</uni-list-item>
			</uni-list>
data() {
			return {
				currnetArr:uni.getStorageSync(你保存的緩存key值),
				loadingStatus:false,
			}
openURL(path){
				console.log('path',path)
				const _this = this
				if(!this.loadingStatus) {
					this.loadingStatus = true
					setTimeout(() => {
						uni.openDocument({
								filePath: path,
								success: function(res) {
										_this.loadingStatus = false
										console.log('打開文檔成功');
								},
								fail: function(e){
									_this.loadingStatus = false
									uni.showToast({
										icon: 'none',
										mask: true,
										title: '文件打開失敗',
									});
								}
						});
					}, 1000)
				}
				
				
			},

問題描述

通過這種uni.downloadFile配合uni.saveFile下載并保存文件,然后在下載中心點(diǎn)擊打開文件,邏輯是沒問題的。但是這個(gè)方式蘋果手機(jī)可以正常打開文件,安卓一直打開文件報(bào)錯(cuò)。

原因分析:

分析是download方法的問題,保存文件一般是內(nèi)部復(fù)制,系統(tǒng)差異應(yīng)該是保存路徑的問題, 即uni.saveFile返回的savedFilePath保存的臨時(shí)文件路徑問題

解決方案:

配合H5+的下載方法,強(qiáng)行指定下載路徑,這樣就不會(huì)有臨時(shí)路徑的差異了,從而解決安卓系統(tǒng)打不開文件問題,下面是下載文件最終版本代碼

uni.downloadFile({
					     method: 'GET',
					     url: 你的url,
					     timeout: 2000,
					    header:{
					      authorization:你的 token,
					    },
					     success: (data) => {
					             if (data.statusCode === 200) {
									 plus.io.resolveLocalFileSystemURL( data.tempFilePath, function( entry ) {
										 const name = `${entry.name}.pdf` //這里設(shè)置文件名根據(jù)自己的下載文件后綴來修改,我這邊需求是pdf
										 entry.getParent(function(scb) {
											 entry.moveTo(scb,name, function(sEntry) {
												 //文件保存到本地
												uni.saveFile({
														tempFilePath: sEntry.fullPath, //臨時(shí)路徑
														success: function(res) {
																// 判斷手機(jī)平臺是 Android 還是 IOS
																if (uni.getSystemInfoSync().platform === 'android') {
																		uni.showModal({
																				title:"保存地址為:",
																				content: res.savedFilePath,
																				duration: 3000,
																		})
																} else {
																		uni.showModal({
																				icon: 'none',
																				title: '文件已保存:',
																				content: res.savedFilePath,
																				duration: 3000,
																		});
																}
																
																let list = uni.getStorageSync("__local_down_history");
																if (list.length) {
																	let arrNew=list
																	let newObj={
																		path:res.savedFilePath,
																		name:"收據(jù)."+ReceiptNo+'.pdf' 
																		//這里的保存的name是下載中心展示的文件名,不是這個(gè)文件原本的名字
																	}
																	arrNew.unshift(newObj);
																	_this.localSearchList=arrNew
																	// arrUnique(this.localSearchList);
																} else {
																	_this.localSearchList = [{
																		path:res.savedFilePath,
																		name:"收據(jù)."+ReceiptNo+'.pdf'
																	}];
																}
																console.log("文件緩存",_this.localSearchList)
																uni.setStorageSync("__local_down_history", _this.localSearchList);        
														}
												});
											 } )
										 })
									 })
					                     
					             }
					     },
					     fail: (err) => {
					             console.log(err);
					             uni.showToast({
					                     icon: 'none',
					                     mask: true,
					                     title: '失敗請重新下載',
					             });
					     },
					 })

總結(jié) 

到此這篇關(guān)于uni-app使用uni-download和uni.saveFile下載保存文件遇到的問題及解決方法的文章就介紹到這了,更多相關(guān)uni-app下載保存文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論