From e9b9ac1f7aa633e30029b5d346cf27e3e7524dd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A2=E8=B6=8A?= <552369664@qq.com> Date: Mon, 28 Oct 2024 17:04:57 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E3=80=91=E4=BF=AE=E5=A4=8D=E7=9C=9F=E6=9C=BA=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E5=9B=BE=E7=89=87=E4=B8=8A=E4=BC=A0=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../s-uploader/choose-and-upload-file.js | 66 +++++++++++-------- sheep/components/s-uploader/s-uploader.vue | 37 +++++------ sheep/components/s-uploader/utils.js | 5 +- 3 files changed, 57 insertions(+), 51 deletions(-) diff --git a/sheep/components/s-uploader/choose-and-upload-file.js b/sheep/components/s-uploader/choose-and-upload-file.js index d3c5bccc..9c7036cb 100644 --- a/sheep/components/s-uploader/choose-and-upload-file.js +++ b/sheep/components/s-uploader/choose-and-upload-file.js @@ -204,32 +204,46 @@ async function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) { // 如果是前端直连上传 if (UPLOAD_TYPE.CLIENT === import.meta.env.SHOPRO_UPLOAD_TYPE) { - for (const file of files) { - // 1.1 获取文件预签名地址 - const { data: presignedInfo } = await FileApi.getFilePresignedUrl(file.name); - // 1.2 获取二进制文件对象 - const fileBuffer = await convertToArrayBuffer(file); - // 1.3 上传文件 - await uni.request({ - url: presignedInfo.uploadUrl, // 预签名的上传 URL - method: 'PUT', // 使用 PUT 方法 - header: { - 'Content-Type': file.fileType + '/' + file.name.substring(file.name.lastIndexOf('.') + 1), // 设置内容类型 - }, - data: fileBuffer, // 文件的路径,适用于小程序 - success: (res) => { - // 1.4. 记录文件信息到后端(异步) - createFile(presignedInfo, file); - // 1.5. 重新赋值 - file.url = presignedInfo.url; - console.log('上传成功:', res); - }, - fail: (err) => { - console.error('上传失败:', err); - }, - }); - } - return files; + // 为上传创建一组 Promise + const uploadPromises = files.map(async (file) => { + try { + // 1.1 获取文件预签名地址 + const { data: presignedInfo } = await FileApi.getFilePresignedUrl(file.name); + // 1.2 获取二进制文件对象 + const fileBuffer = await convertToArrayBuffer(file); + + // 返回上传的 Promise + return new Promise((resolve, reject) => { + uni.request({ + url: presignedInfo.uploadUrl, // 预签名的上传 URL + method: 'PUT', // 使用 PUT 方法 + header: { + 'Content-Type': + file.fileType + '/' + file.name.substring(file.name.lastIndexOf('.') + 1), // 设置内容类型 + }, + data: fileBuffer, // 文件的路径,适用于小程序 + success: (res) => { + // 1.4. 记录文件信息到后端(异步) + createFile(presignedInfo, file); + // 1.5. 重新赋值 + file.url = presignedInfo.url; + console.log('上传成功:', res); + resolve(file); + }, + fail: (err) => { + console.error('上传失败:', err); + reject(err); + }, + }); + }); + } catch (error) { + console.error('上传失败:', error); + throw error; + } + }); + + // 等待所有上传完成 + return await Promise.all(uploadPromises); // 返回已上传的文件列表 } else { // 后端上传 for (let file of files) { diff --git a/sheep/components/s-uploader/s-uploader.vue b/sheep/components/s-uploader/s-uploader.vue index a459d84f..e0a37e34 100644 --- a/sheep/components/s-uploader/s-uploader.vue +++ b/sheep/components/s-uploader/s-uploader.vue @@ -44,13 +44,7 @@