Skip to content

Commit

Permalink
feat(Upload): 文件列表支持显示上传失败的原因
Browse files Browse the repository at this point in the history
  • Loading branch information
sheepluo committed Nov 5, 2023
1 parent a93f68d commit 35fc954
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
19 changes: 15 additions & 4 deletions js/upload/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
handleSuccessParams,
UploadTriggerUploadText,
ErrorContext,
ResponseType,
} from './types';

export interface BeforeUploadExtra {
Expand Down Expand Up @@ -67,13 +68,15 @@ export interface OnErrorParams extends ErrorContext {

export function handleError(options: OnErrorParams) {
const { event, files, response, XMLHttpRequest, formatResponse } = options;
files.forEach((file) => {
file.status = 'fail';
});
let res = response;
if (isFunction(formatResponse)) {
res = formatResponse(response, { file: files[0], currentFiles: files });
}
files.forEach((file) => {
file.status = 'fail';
file.response = res;
});

Check failure on line 79 in js/upload/main.ts

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
return { response: res, event, files, XMLHttpRequest };
}

Expand Down Expand Up @@ -146,7 +149,15 @@ export function uploadOneRequest(params: HandleUploadParams): Promise<UploadRequ
resolve({});
return;
}
const { response = {} } = res;
let response = res.response as ResponseType;
if (isFunction(params.formatResponse)) {
response = params.formatResponse(response, { file: toUploadFiles[0], currentFiles: toUploadFiles });
if (response.status === 'success' && !response.files) {
log.error('Upload', 'formatResponse:: upload success `files` are missing');
} else if (!response.status) {
log.error('Upload', 'formatResponse: return value of `status` is missing');
}
}
if (res.status === 'fail') {
response.error = res.error || response.error;
}
Expand Down
2 changes: 1 addition & 1 deletion js/upload/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export interface OnResponseErrorContext {
files: UploadFile[];
}

export type ResponseType = { error?: string; url?: string } & Record<string, any>;
export type ResponseType = { error?: string; url?: string; status?: 'fail' | 'success'; files?: UploadFile[] } & Record<string, any>

export interface HandleUploadParams {
/** 已经上传过的文件 */
Expand Down

0 comments on commit 35fc954

Please sign in to comment.