uniapp 下載文件到指定目錄中及重新命名
uniapp 下載前需要開啟app 應用讀取權限
開啟方式
手動開啟
uniapp 權限中添加讀取權限
添加一下代碼即可
//var data={mid='123456',title:'文件名稱' } xiazhai(data, index) { var downFileName = data.title + '.mp3'; var filePath = "file://storage/emulated/0/Vpi視頻/" + downFileName; // 檢查文件是否已存在 plus.io.resolveLocalFileSystemURL(filePath, function(entry) { // 文件已存在,不進行下載 uni.showToast({ icon: 'none', mask: true, title: '文件已存在,無需重復下載', duration: 2000 }); }, function() { uni.showLoading({ title: '正在下載' }); var downFileName = data.title + '.mp3'; var url = 'http://music.163.com/song/media/outer/url?id=' + data.mid + '.mp3'; let dtask = plus.downloader.createDownload(url, { filename: "file://storage/emulated/0/Vpi視頻/" + downFileName //利用保存路徑,實現(xiàn)下載文件的重命名 }, (d, status) => { //d為下載的文件對象 if (status == 200) { uni.hideLoading(); uni.showToast({ icon: 'none', mask: true, title: '已保存到文件夾:/Vpi視頻/' + newFileName, //保存路徑 duration: 3000, }); //下載成功,d.filename是文件在保存在本地的相對路徑,使用下面的API可轉為平臺絕對路徑 let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename); setTimeout(() => { plus.runtime.openFile(d.filename); //選擇軟件打開文件 }, 1500) } else { //下載失敗 uni.hideLoading(); uni.showToast({ icon: 'none', title: '下載失敗,請稍后重試', }); plus.downloader.clear(); //清除下載任務 } }) dtask.start(); }); },