!116 【功能修复】修复真机环境下的图片上传问题

Merge pull request !116 from 卢越/master
pull/117/head
芋道源码 2024-10-28 11:14:29 +00:00 committed by Gitee
commit f94df74591
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 57 additions and 51 deletions

View File

@ -204,17 +204,22 @@ async function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) {
// 如果是前端直连上传 // 如果是前端直连上传
if (UPLOAD_TYPE.CLIENT === import.meta.env.SHOPRO_UPLOAD_TYPE) { if (UPLOAD_TYPE.CLIENT === import.meta.env.SHOPRO_UPLOAD_TYPE) {
for (const file of files) { // 为上传创建一组 Promise
const uploadPromises = files.map(async (file) => {
try {
// 1.1 获取文件预签名地址 // 1.1 获取文件预签名地址
const { data: presignedInfo } = await FileApi.getFilePresignedUrl(file.name); const { data: presignedInfo } = await FileApi.getFilePresignedUrl(file.name);
// 1.2 获取二进制文件对象 // 1.2 获取二进制文件对象
const fileBuffer = await convertToArrayBuffer(file); const fileBuffer = await convertToArrayBuffer(file);
// 1.3 上传文件
await uni.request({ // 返回上传的 Promise
return new Promise((resolve, reject) => {
uni.request({
url: presignedInfo.uploadUrl, // 预签名的上传 URL url: presignedInfo.uploadUrl, // 预签名的上传 URL
method: 'PUT', // 使用 PUT 方法 method: 'PUT', // 使用 PUT 方法
header: { header: {
'Content-Type': file.fileType + '/' + file.name.substring(file.name.lastIndexOf('.') + 1), // 设置内容类型 'Content-Type':
file.fileType + '/' + file.name.substring(file.name.lastIndexOf('.') + 1), // 设置内容类型
}, },
data: fileBuffer, // 文件的路径,适用于小程序 data: fileBuffer, // 文件的路径,适用于小程序
success: (res) => { success: (res) => {
@ -223,13 +228,22 @@ async function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) {
// 1.5. 重新赋值 // 1.5. 重新赋值
file.url = presignedInfo.url; file.url = presignedInfo.url;
console.log('上传成功:', res); console.log('上传成功:', res);
resolve(file);
}, },
fail: (err) => { fail: (err) => {
console.error('上传失败:', err); console.error('上传失败:', err);
reject(err);
}, },
}); });
});
} catch (error) {
console.error('上传失败:', error);
throw error;
} }
return files; });
// 等待所有上传完成
return await Promise.all(uploadPromises); // 返回已上传的文件列表
} else { } else {
// 后端上传 // 后端上传
for (let file of files) { for (let file of files) {

View File

@ -44,13 +44,7 @@
<script> <script>
import { chooseAndUploadFile, uploadCloudFiles } from './choose-and-upload-file.js'; import { chooseAndUploadFile, uploadCloudFiles } from './choose-and-upload-file.js';
import { import { get_extname, get_files_and_is_max, get_file_data } from './utils.js';
get_file_ext,
get_extname,
get_files_and_is_max,
get_file_info,
get_file_data,
} from './utils.js';
import uploadImage from './upload-image.vue'; import uploadImage from './upload-image.vue';
import uploadFile from './upload-file.vue'; import uploadFile from './upload-file.vue';
import sheep from '@/sheep'; import sheep from '@/sheep';
@ -352,11 +346,10 @@
/** /**
* 选择文件并上传 * 选择文件并上传
*/ */
chooseFiles() { async chooseFiles() {
const _extname = get_extname(this.fileExtname); const _extname = get_extname(this.fileExtname);
// //
uniCloud await chooseAndUploadFile({
.chooseAndUploadFile({
type: this.fileMediatype, type: this.fileMediatype,
compressed: false, compressed: false,
sizeType: this.sizeType, sizeType: this.sizeType,
@ -587,7 +580,7 @@
path: v.path, path: v.path,
size: v.size, size: v.size,
fileID: v.fileID, fileID: v.fileID,
url: v.path, url: v.url,
}); });
}); });
return newFilesData; return newFilesData;

View File

@ -17,12 +17,11 @@ export const get_file_ext = (name) => {
*/ */
export const get_extname = (fileExtname) => { export const get_extname = (fileExtname) => {
if (!Array.isArray(fileExtname)) { if (!Array.isArray(fileExtname)) {
let extname = fileExtname.replace(/(\[|\])/g, ''); let extname = fileExtname.replace(/([\[\]])/g, '');
return extname.split(','); return extname.split(',');
} else { } else {
return fileExtname; return fileExtname;
} }
return [];
}; };
/** /**
@ -91,7 +90,7 @@ export const get_file_data = async (files, type = 'image') => {
extname: extname || '', extname: extname || '',
cloudPath: files.cloudPath, cloudPath: files.cloudPath,
fileType: files.fileType, fileType: files.fileType,
url: files.path || files.path, url: files.url || files.path,
size: files.size, //单位是字节 size: files.size, //单位是字节
image: {}, image: {},
path: files.path, path: files.path,