微信小程序之下載(下載文件tempFilePath后綴unknown)


1.下載文件

使用downloadFile下載API

/* 直接這樣寫的話,會出現下載文件后綴名為unknown的情況 */ wx.downloadFile({ url: '', success (res) { console.log(res.filePath); } })

改進后的下載保存

/* 使用時間戳為下載后的文件的名字 */ let fileName = new Date().valueOf(); wx.downloadFile({ /* url 為要下載的文件鏈接 */ url: '', /* filePath指定文件下載后存儲的路徑,wx.env.USER_DATA_PATH */ filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4', success: res => { let filePath = res.filePath; wx.saveVideoToPhotosAlbum({ filePath, success: file => { wx.hideLoading(); /* 刪除緩存 */ let fileMgr = wx.getFileSystemManager(); fileMgr.unlink({ filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4', success: function (r) { }, }) }, fail: err => { wx.hideLoading(); /* 拒絕授權時顯示 */ if (err.errMsg === 'saveVideoToPhotosAlbum:fail auth deny') { wx.showModal({ title: '提示', content: '需要您授權保存相冊', showCancel: false, success: data => { /* 打開權限設置 */ wx.openSetting({ success: setting => { if (setting.authSetting['scope.writePhotosAlbum']) { wx.showModal({ title: '提示', content: '獲取權限成功,再次點擊下載即可保存', showCancel: false, }) } else { wx.showModal({ title: '提示', content: '獲取權限失敗,將無法保存到相冊哦', showCancel: false, }) } }, }) } }) } }, }, fail: err => { wx.hideLoading(); if (err.errMsg == 'downloadFile:fail createDownloadTask:fail url not in domain list') { wx.showToast({ title: '服務器錯誤,請聯系相關管理員', icon: 'none' }) } }, complete: () => { wx.hideLoading(); } })

資源搜索網站大全 https://www.renrenfan.com.cn 廣州VI設計公司https://www.houdianzi.com

2.下載圖片

下載圖片的步驟和上面是一樣的,就是在保存的時候,api使用的不一樣

 
 wx.saveImageToPhotosAlbum({ filePath: '', success: () => {} })


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM