From 2a1a6360ca763139b666aaca899703931a4a672b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B6=A6=E5=B1=B1?= Date: Wed, 23 May 2018 11:23:00 +0800 Subject: [PATCH] Upload: fix Error when `beforeUpload` hook return promise of file object (#11297) * Upload: fix beforeUpload hook bug * Upload: add ElUploadInternalRawFile interface --- packages/upload/src/upload.vue | 12 ++++++++++-- types/upload.d.ts | 8 ++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/upload/src/upload.vue b/packages/upload/src/upload.vue index 485486182..200ca0ac9 100644 --- a/packages/upload/src/upload.vue +++ b/packages/upload/src/upload.vue @@ -94,8 +94,16 @@ export default { const fileType = Object.prototype.toString.call(processedFile); if (fileType === '[object File]' || fileType === '[object Blob]') { - processedFile.name = rawFile.name; - processedFile.uid = rawFile.uid; + if (fileType === '[object Blob]') { + processedFile = new File([processedFile], rawFile.name, { + type: rawFile.type + }); + } + for (const p in rawFile) { + if (rawFile.hasOwnProperty(p)) { + processedFile[p] = rawFile[p]; + } + } this.post(processedFile); } else { this.post(rawFile); diff --git a/types/upload.d.ts b/types/upload.d.ts index f48023303..92e6f18ff 100644 --- a/types/upload.d.ts +++ b/types/upload.d.ts @@ -9,13 +9,17 @@ export interface FileListItem { status?: FileUploadStatus } +export interface ElUploadInternalRawFile extends File { + uid: number +} + export interface ElUploadInternalFileDetail { status: FileUploadStatus, name: string, size: number, percentage: number, uid: number, - raw: File, + raw: ElUploadInternalRawFile, url?: string } @@ -83,7 +87,7 @@ export declare class ElUpload extends ElementUIComponent { onChange: (file: ElUploadInternalFileDetail, fileList: ElUploadInternalFileDetail[]) => void /** Hook function before uploading with the file to be uploaded as its parameter. If false or a Promise is returned, uploading will be aborted */ - beforeUpload: (file: ElUploadInternalFileDetail) => boolean | Promise + beforeUpload: (file: ElUploadInternalRawFile) => boolean | Promise /** Whether thumbnail is displayed */ thumbnailMode: boolean