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

feat(Upload): 文件列表支持显示上传失败的原因 #1638

Merged
merged 2 commits into from
Nov 5, 2023
Merged
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
18 changes: 14 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,14 @@ 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;
});
return { response: res, event, files, XMLHttpRequest };
}

Expand Down Expand Up @@ -146,7 +148,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
Loading