edu/uniapp/src/utils/file.ts

21 lines
570 B
TypeScript
Raw Normal View History

2022-09-08 08:28:56 +00:00
export async function saveImageToPhotosAlbum(url: string) {
2022-09-29 06:41:46 +00:00
if (!url) return uni.$u.toast('图片错误')
2022-09-08 08:28:56 +00:00
//#ifdef H5
2022-09-29 06:41:46 +00:00
uni.$u.toast('长按图片保存')
2022-09-08 08:28:56 +00:00
//#endif
2022-09-29 06:41:46 +00:00
//#ifndef H5
2022-09-08 08:28:56 +00:00
try {
const res: any = await uni.downloadFile({ url, timeout: 10000 })
await uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath
})
uni.showToast({
title: '保存成功',
icon: 'success'
})
} catch (error: any) {
2022-09-20 03:06:54 +00:00
uni.$u.toast(error.errMsg || '保存失败')
2022-09-08 08:28:56 +00:00
}
2022-09-29 06:41:46 +00:00
//#endif
2022-09-08 08:28:56 +00:00
}