Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(uploader): 修复data外部修改后提交时依然是旧值问题 #3223

Open
wants to merge 1 commit into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/packages/__VUE/uploader/doc.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/packages/__VUE/uploader/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ const clearUpload = () => {
| name | 文件名称 | - |
| url | 文件路径 | - |
| type | 文件类型 | `"image/jpeg"` |
| formData | 上传所需的 data | `new FormData() ` |
| percentage | 上传百分比 | `0` |
| sourceFile | 上传所需的 file | - |

### Events

Expand Down
2 changes: 1 addition & 1 deletion src/packages/__VUE/uploader/doc.taro.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ const clearUpload = () => {
| uid | 文件的唯一标识 | `new Date().getTime().toString()` |
| name | 文件名称 | - |
| url | 文件路径 | - |
| formData | 上传所需的 data | `{}` |
| percentage | 上传百分比 | `0` |
| sourceFile | 上传所需的 file(仅WEB) | - |

### Events

Expand Down
24 changes: 15 additions & 9 deletions src/packages/__VUE/uploader/index.taro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 9 additions & 13 deletions src/packages/__VUE/uploader/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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] <Uploader> formData.get(name)', error)
}
uploadOption.sourceFile = fileItem.sourceFile
uploadOption.onStart = (option: UploadOptions) => {
fileItem.status = 'ready'
fileItem.message = translate('readyUpload')
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/packages/__VUE/uploader/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export class FileItem {
type?: string
path?: string
percentage: string | number = 0
formData: any = {}
sourceFile?: File
}