目前寫的是一個兼容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'
})
}
});
}
}
});
},