uniapp 跨平臺 H5 小程序 文件下載

目前寫的是一個兼容H5與小程序的項目。大多數內容還是可以比較完美的兼容的。但是到了下載文件這裏就出了點問題。
uni.downloadFile雖然是可以兼容小程序和H5,但是uni.saveFile是不支持的。
這裏就把兩端分開寫了。

HTML

									<!-- #ifdef H5 -->
									<a class="table-btn" href="http://xxxxxxxxxx" target='_blank'>下載入口</a>
									<!-- #endif -->
									
									<!-- #ifdef MP-WEIXIN -->
									<view class="table-btn" @click="downLoad(item.id)">下載入口</view>
									<!-- #endif -->

JS

			/**
			 * 下載到本地
			 */
			downLoad: function(id) {
				uni.downloadFile({
					url: 'http://xxxxxxxxxx?id=' + id, //僅爲示例,並非真實的資源
					success: (e) => {
						if (e.statusCode === 200) {
							let tempFilePaths = e.tempFilePath;
							console.log('地址', tempFilePaths)
							/*注意:saveFile 會把臨時文件移動,因此調用成功後傳入的 tempFilePath 將不可用。*/
							uni.saveFile({
								tempFilePath: tempFilePaths,
								complete: (res) => {
									var savedFilePath = res.savedFilePath;
									console.log(savedFilePath);
									uni.showToast({
										title: '保存成功',
										icon: 'none'
									})
								}
							});
						}
					}
				});
			},
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章