天天看點

uniapp實作預覽請求背景接口傳回的檔案

業務需求:請求背景接口 然後打開接口傳回的流檔案

一般有兩種方法可以實作:

1.下載下傳成功後 直接通過uni.openDocument(OBJECT)打開

uni.downloadFile({
					url: this.url, //僅為示例,并非真實的資源
					// 根據業務修改header
					headers: {
						Authorization: token,
						refreshToken: refreshToken,
					},
					success: (res) => {
					// 根據接口來處理  接口都會傳回檔案格式 一定要有
						const fileType = res?.header?.filename?.split(".")[1]
						if (res.statusCode === 200) {
							uni.openDocument({
								filePath: res.tempFilePath,
								fileType,
							})
						}
					}
				});
           

2.下載下傳成功後用webview打開

uni.downloadFile({
					url: this.url, //僅為示例,并非真實的資源
					// 根據業務修改header
					headers: {
						Authorization: token,
						refreshToken: refreshToken,
					},
					success: (res) => {
						if (res.statusCode === 200) {
							const fileType = res?.header?.filename?.split(".")[1],
						           preUrl=res.tempFilePath
							// 跳轉到webview頁面 可以實作不離開本應用預覽檔案
							uni.navigateTo({
								url: `/pages/webview?fileType=${fileType}&url=${preUrl}`
							});
						}
					}
				});
           

繼續閱讀