保存图片到相册时报类型不存在的错:fail inval file type?
原因是在调用wx.downloadFile时返回的tempFilepath文件路径的后缀为unknown,导致传给saveFile的路径也没有后缀,所以真机上保存图片失败
以下是我封装的代码:
import { BASE_URL } from '@/libs/config'
let url = ''
export default function downLoad (fileId) {
url = `${BASE_URL}/file/download/${fileId}`
getSet()
}
function getSet () {
uni.getSetting({
success: (res) => {
if (res.authSetting['scope.writePhotosAlbum']) {
// uni.downloadFile
uni.downloadFile({
url: url,
header: {
'Content-Type': 'png/jpeg'
},
success: (infos) => {
console.log('[get download file]', infos)
saveFile(infos.tempFilePath)
},
fail: () => {
saveFail()
}
})
} else {
author()
}
},
fail: () => {
saveFail()
}
})
}
function author () {
uni.authorize({
scope: 'scope.writePhotosAlbum',
success: (res) => {
getSet()
},
fail: () => {
saveFail()
}
})
}
function saveFile (path) {
uni.saveImageToPhotosAlbum({
filePath: path,
success: (res) => {
uni.showToast('保存图片到相册成功')
console.log('[download file success]', res)
},
fail: (err) => {
console.log('[got error when save file]', err)
if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
getSet()
} else {
saveFail()
}
}