allow pickupFile2Cloud for all file types

This commit is contained in:
陆柯 2022-10-30 19:58:17 +08:00
parent adb2a96287
commit 74826d0ac6

View File

@ -342,10 +342,7 @@ export default {
let filePath, let filePath,
cloudPath, cloudPath,
systemInfo = uni osName = uni.getSystemInfoSync().osName
.getSystemInfoSync()
.system.toLowerCase()
.replace(/\s+/g, '')
if (mediaType === 'image') { if (mediaType === 'image') {
let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType }) let [errorChoose, { tempFilePaths, tempFiles } = {}] = await uni.chooseImage({ count, sizeType, sourceType })
// uni.showModal({ title: 'tempFilePaths[0]=' + tempFilePaths[0] }) // uni.showModal({ title: 'tempFilePaths[0]=' + tempFilePaths[0] })
@ -353,37 +350,44 @@ export default {
// #ifndef H5 // #ifndef H5
// let [errorGetImageInfo, { path, width, height, orientation, type }] = await uni.getImageInfo({ src: filePath }) // let [errorGetImageInfo, { path, width, height, orientation, type }] = await uni.getImageInfo({ src: filePath })
// cloudPath = path // 完整路径,包含后缀名。形如 file:///var/mobile/Containers/Data/Application/55A76332-44F5-4D5F-A9F6-3F857D584883/Documents/Pandora/apps/D064A425A8BEC13F9D8F741B98B37BC5/doc/uniapp_temp_1598593902955/compressed/1598593925815.png // cloudPath = path // 完整路径,包含后缀名。形如 file:///var/mobile/Containers/Data/Application/55A76332-44F5-4D5F-A9F6-3F857D584883/Documents/Pandora/apps/D064A425A8BEC13F9D8F741B98B37BC5/doc/uniapp_temp_1598593902955/compressed/1598593925815.png
cloudPath = `APP_${systemInfo}__${filePath}` // 在 iOS 上是 "_doc/uniapp_temp_1598593902955/compressed/1598593925815.png", 有时还包含从 file:/// 开始的完整路径名 cloudPath = `APP_${osName}__${filePath}` // 在 iOS 上是 "_doc/uniapp_temp_1598593902955/compressed/1598593925815.png", 有时还包含从 file:/// 开始的完整路径名
// #endif // #endif
// #ifdef H5 // #ifdef H5
cloudPath = `H5_${systemInfo}_${this.parse_ua_to_browser()}__${tempFiles[0].name}` // name is available in H5 only. 只包含文件名和后缀名,不包含路径。 cloudPath = `H5_${osName}_${this.parse_ua_to_browser()}__${tempFiles[0].name}` // name is available in H5 only. 只包含文件名和后缀名,不包含路径。
// #endif // #endif
} else if (mediaType === 'video') { } else if (mediaType === 'video') {
let [errorChoose, { tempFilePath, tempFile, duration, size, width, height, name }] = await uni.chooseVideo({ sourceType, maxDuration }) let [errorChoose, { tempFilePath, tempFile, duration, size, width, height, name }] = await uni.chooseVideo({ sourceType, maxDuration })
// uni.showModal({ title: 'tempFilePath=' + tempFilePath }) // uni.showModal({ title: 'tempFilePath=' + tempFilePath })
filePath = tempFilePath // 在 iOS 上形如 "file:///var/mobile/Containers/Data/Application/55A76332-44F5-4D5F-A9F6-3F857D584883/Documents/Pandora/apps/26B43CD2F587D37FC6799108434A6F84/doc/uniapp_temp_1598596171580/gallery/IMG_3082.MOV" filePath = tempFilePath // 在 iOS 上形如 "file:///var/mobile/Containers/Data/Application/55A76332-44F5-4D5F-A9F6-3F857D584883/Documents/Pandora/apps/26B43CD2F587D37FC6799108434A6F84/doc/uniapp_temp_1598596171580/gallery/IMG_3082.MOV"
// #ifndef H5 // #ifndef H5
cloudPath = `APP_${systemInfo}_dur${duration}__${filePath}` cloudPath = `APP_${osName}_dur${duration}__${filePath}`
// #endif // #endif
// #ifdef H5 // #ifdef H5
cloudPath = `H5_${systemInfo}_${this.parse_ua_to_browser()}_dur${duration}__${name}` // tempFile and name are H5 only cloudPath = `H5_${osName}_${this.parse_ua_to_browser()}_dur${duration}__${name}` // tempFile and name are H5 only
// #endif // #endif
// iOS 上测试filePath 为 *.MOV而阿里云只允许 *.mp4, 所以添加 .mp4 后缀。参见 https://uniapp.dcloud.net.cn/uniCloud/storage?id=clouduploadfile // iOS 上测试filePath 为 *.MOV而阿里云只允许 *.mp4, 所以添加 .mp4 后缀。参见 https://uniapp.dcloud.net.cn/uniCloud/storage?id=clouduploadfile
// 20200915测试阿里云支持上传 *.mov 了。 // 20200915测试阿里云支持上传 *.mov 了。
if (!/\.(mp4|mov)$/i.test(cloudPath)) cloudPath = cloudPath + '.mp4' if (!/\.(mp4|mov)$/i.test(cloudPath)) cloudPath = cloudPath + '.mp4'
} else { } else {
return { _state: 'CLIENT_FAIL_UNKNOWN_MEDIA_TYPE' } let { errMsg, tempFilePaths, tempFiles } = await uniCloud.chooseAndUploadFile({
} type: 'all',
count: 1,
if (process.env.NODE_ENV !== 'production') { // extention: [],
cloudPath = 'dev_' + cloudPath // onChooseFile: ({ errMsg, tempFilePaths, tempFiles }) => { },
// onUploadProgress: ({ index, loaded, total, tempFilePath, tempFile }) => { }
})
if (errMsg === 'chooseAndUploadFile:ok') {
return { _state: 'SUCCESS', fileUrl: tempFiles?.[0]?.url }
} else {
return { _state: 'CLIENT_FAIL_chooseAndUploadFile' }
}
} }
if (filePath) { if (filePath) {
uni.showLoading() uni.showLoading()
const { fileID, requestId } = await uniCloud.uploadFile({ const { fileID, requestId } = await uniCloud.uploadFile({
filePath: filePath, filePath: filePath,
cloudPath: cloudPath, // 关键是要具有文件格式后缀名,这样可以保持阿里云下载链接也用这个后缀名。 cloudPath: process.env.NODE_ENV !== 'production' ? 'dev_' + cloudPath : cloudPath, // 关键是要具有文件格式后缀名,这样可以保持阿里云下载链接也用这个后缀名。
fileType: mediaType, // = image, video, audio fileType: mediaType, // = image, video, audio
onUploadProgress: function (progressEvent) { onUploadProgress: function (progressEvent) {
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total) var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
@ -393,9 +397,11 @@ export default {
if (fileID) { if (fileID) {
return { _state: 'SUCCESS', fileUrl: fileID, requestId } return { _state: 'SUCCESS', fileUrl: fileID, requestId }
} else {
return { _state: 'CLIENT_WOBASE_FAIL_uploadFile' }
} }
} }
return { _state: 'CLIENT_FAIL_CHOOSE_FILE' } return { _state: 'CLIENT_FAIL_chooseFile' }
}, },
async pickupFile ({ async pickupFile ({