Skip to content

Commit 63fa320

Browse files
authored
fix: event return parsed file (#307)
1 parent d385e68 commit 63fa320

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

src/AjaxUploader.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface ParsedFileInfo {
1919
origin: RcFile;
2020
action: string;
2121
data: object;
22-
parsedFile: Exclude<BeforeUploadFileType, boolean>;
22+
parsedFile: RcFile;
2323
}
2424

2525
class AjaxUploader extends Component<UploadProps> {
@@ -162,23 +162,28 @@ class AjaxUploader extends Component<UploadProps> {
162162
mergedData = data;
163163
}
164164

165-
const parsedFile =
165+
const parsedData =
166166
// string type is from legacy `transformFile`.
167167
// Not sure if this will work since no related test case works with it
168168
(typeof transformedFile === 'object' || typeof transformedFile === 'string') &&
169169
transformedFile
170170
? transformedFile
171171
: file;
172172

173-
// Used for `request.ts` get form data name
174-
if (typeof parsedFile === 'object' && !(parsedFile as any).name) {
175-
(parsedFile as any).name = file.name;
173+
let parsedFile: File;
174+
if (parsedData instanceof File) {
175+
parsedFile = parsedData;
176+
} else {
177+
parsedFile = new File([parsedData], file.name, { type: file.type });
176178
}
177179

180+
const mergedParsedFile: RcFile = parsedFile as RcFile;
181+
mergedParsedFile.uid = file.uid;
182+
178183
return {
179184
origin: file,
180185
data: mergedData,
181-
parsedFile,
186+
parsedFile: mergedParsedFile,
182187
action: mergedAction,
183188
};
184189
};
@@ -203,17 +208,17 @@ class AjaxUploader extends Component<UploadProps> {
203208
method: method || 'post',
204209
onProgress: (e: UploadProgressEvent) => {
205210
const { onProgress } = this.props;
206-
onProgress?.(e, origin);
211+
onProgress?.(e, parsedFile);
207212
},
208213
onSuccess: (ret: any, xhr: XMLHttpRequest) => {
209214
const { onSuccess } = this.props;
210-
onSuccess?.(ret, origin, xhr);
215+
onSuccess?.(ret, parsedFile, xhr);
211216

212217
delete this.reqs[uid];
213218
},
214219
onError: (err: UploadRequestError, ret: any) => {
215220
const { onError } = this.props;
216-
onError?.(err, ret, origin);
221+
onError?.(err, ret, parsedFile);
217222

218223
delete this.reqs[uid];
219224
},

tests/uploader.spec.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -463,20 +463,7 @@ describe('uploader', () => {
463463
});
464464

465465
describe('onBatchStart', () => {
466-
const files = [
467-
{
468-
name: 'bamboo.png',
469-
toString() {
470-
return this.name;
471-
},
472-
},
473-
{
474-
name: 'light.png',
475-
toString() {
476-
return this.name;
477-
},
478-
},
479-
];
466+
const files = [new File([], 'bamboo.png'), new File([], 'light.png')];
480467

481468
const batchEventFiles = files.map(file =>
482469
expect.objectContaining({

0 commit comments

Comments
 (0)