From d785514a83a6534e8ca8974e53cf797805a426d1 Mon Sep 17 00:00:00 2001 From: xiaohe0601 Date: Mon, 11 Nov 2024 14:16:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(uploader):=20=E4=BF=AE=E5=A4=8Ddata?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E4=BF=AE=E6=94=B9=E5=90=8E=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E6=97=B6=E4=BE=9D=E7=84=B6=E6=98=AF=E6=97=A7=E5=80=BC=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/packages/__VUE/uploader/doc.en-US.md | 2 +- src/packages/__VUE/uploader/doc.md | 2 +- src/packages/__VUE/uploader/doc.taro.md | 2 +- src/packages/__VUE/uploader/index.taro.vue | 24 ++++++++++++++-------- src/packages/__VUE/uploader/index.vue | 22 ++++++++------------ src/packages/__VUE/uploader/type.ts | 2 +- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/packages/__VUE/uploader/doc.en-US.md b/src/packages/__VUE/uploader/doc.en-US.md index bd2ef4d317..0d97cacab2 100644 --- a/src/packages/__VUE/uploader/doc.en-US.md +++ b/src/packages/__VUE/uploader/doc.en-US.md @@ -366,8 +366,8 @@ const clearUpload = () => { | name | File name | - | | url | File path | - | | type | File type | `"image/jpeg" ` | -| formData | Upload the required data | `new FormData()` | | percentage | Upload percentage | `0` | +| sourceFile | Upload the required file | - | ### Events diff --git a/src/packages/__VUE/uploader/doc.md b/src/packages/__VUE/uploader/doc.md index 40bc5d686d..cf4bb1fcae 100644 --- a/src/packages/__VUE/uploader/doc.md +++ b/src/packages/__VUE/uploader/doc.md @@ -367,8 +367,8 @@ const clearUpload = () => { | name | 文件名称 | - | | url | 文件路径 | - | | type | 文件类型 | `"image/jpeg"` | -| formData | 上传所需的 data | `new FormData() ` | | percentage | 上传百分比 | `0` | +| sourceFile | 上传所需的 file | - | ### Events diff --git a/src/packages/__VUE/uploader/doc.taro.md b/src/packages/__VUE/uploader/doc.taro.md index 4446d4419e..9e9745438a 100644 --- a/src/packages/__VUE/uploader/doc.taro.md +++ b/src/packages/__VUE/uploader/doc.taro.md @@ -282,8 +282,8 @@ const clearUpload = () => { | uid | 文件的唯一标识 | `new Date().getTime().toString()` | | name | 文件名称 | - | | url | 文件路径 | - | -| formData | 上传所需的 data | `{}` | | percentage | 上传百分比 | `0` | +| sourceFile | 上传所需的 file(仅WEB) | - | ### Events diff --git a/src/packages/__VUE/uploader/index.taro.vue b/src/packages/__VUE/uploader/index.taro.vue index e77670b84b..dcc51a471f 100644 --- a/src/packages/__VUE/uploader/index.taro.vue +++ b/src/packages/__VUE/uploader/index.taro.vue @@ -281,10 +281,21 @@ export default create({ const executeUpload = (fileItem: FileItem, index: number) => { const uploadOption = new UploadOptions() + + if (Taro.getEnv() == 'WEB') { + const formData = new FormData() + for (const [key, value] of Object.entries(props.data)) { + formData.append(key, value) + } + formData.append(props.name, fileItem.sourceFile!) + uploadOption.formData = formData + } else { + uploadOption.formData = props.data as FormData + } + uploadOption.name = props.name uploadOption.url = props.url uploadOption.fileType = fileItem.type - uploadOption.formData = fileItem.formData uploadOption.timeout = (props.timeout as number) * 1 uploadOption.method = props.method uploadOption.xhrState = props.xhrState as number @@ -381,18 +392,13 @@ export default create({ fileItem.status = 'ready' fileItem.message = translate('waitingUpload') fileItem.type = fileType + if (Taro.getEnv() == 'WEB') { - const formData = new FormData() - for (const [key, value] of Object.entries(props.data)) { - formData.append(key, value) - } - formData.append(props.name, file.originalFileObj as Blob) + fileItem.sourceFile = file.originalFileObj fileItem.name = file.originalFileObj?.name fileItem.type = file.originalFileObj?.type - fileItem.formData = formData - } else { - fileItem.formData = props.data } + if (props.isPreview) { fileItem.url = fileType == 'video' ? file.thumbTempFilePath : filepath } diff --git a/src/packages/__VUE/uploader/index.vue b/src/packages/__VUE/uploader/index.vue index f927bc9103..cf72ae1438 100644 --- a/src/packages/__VUE/uploader/index.vue +++ b/src/packages/__VUE/uploader/index.vue @@ -185,20 +185,22 @@ export default create({ } const executeUpload = (fileItem: FileItem, index: number) => { + const formData = new FormData() + for (const [key, value] of Object.entries(props.data)) { + formData.append(key, value) + } + formData.append(props.name, fileItem.sourceFile!) + const uploadOption = new UploadOptions() uploadOption.url = props.url - uploadOption.formData = fileItem.formData + uploadOption.formData = formData uploadOption.timeout = (props.timeout as number) * 1 uploadOption.method = props.method uploadOption.xhrState = props.xhrState as number uploadOption.headers = props.headers uploadOption.withCredentials = props.withCredentials uploadOption.beforeXhrUpload = props.beforeXhrUpload - try { - uploadOption.sourceFile = fileItem.formData.get(props.name) - } catch (error) { - console.warn('[NutUI] formData.get(name)', error) - } + uploadOption.sourceFile = fileItem.sourceFile uploadOption.onStart = (option: UploadOptions) => { fileItem.status = 'ready' fileItem.message = translate('readyUpload') @@ -260,17 +262,11 @@ export default create({ const readFile = (files: File[]) => { files.forEach((file: File, index: number) => { - const formData = new FormData() - for (const [key, value] of Object.entries(props.data)) { - formData.append(key, value) - } - formData.append(props.name, file) - const fileItem = reactive(new FileItem()) fileItem.name = file.name fileItem.status = 'ready' fileItem.type = file.type - fileItem.formData = formData + fileItem.sourceFile = file fileItem.message = translate('waitingUpload') executeUpload(fileItem, index) diff --git a/src/packages/__VUE/uploader/type.ts b/src/packages/__VUE/uploader/type.ts index d6dd03c697..f1f5da11ee 100644 --- a/src/packages/__VUE/uploader/type.ts +++ b/src/packages/__VUE/uploader/type.ts @@ -11,5 +11,5 @@ export class FileItem { type?: string path?: string percentage: string | number = 0 - formData: any = {} + sourceFile?: File }