Skip to content
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ Then open `http://localhost:8000`.

### Methods

| Name | Type | Description |
| ------- | ------------------------ | ------------------------------------ |
| `abort` | `(file: RcFile) => void` | Abort an active upload. |
| `retry` | `(file: RcFile) => void` | Retry an upload for a specific file. |
| Name | Type | Description |
| --- | --- | --- |
| `abort` | `(file: RcFile) => void` | Abort an active upload. |
| `retry` | `(file: RcFile) => void` | Retry an upload for a specific file. Only works for files that have been uploaded at least once (have cached info). Reuses the processed `beforeUpload`, `action`, and `data` from the first attempt. If no cache is found, fires `onError` with `UploadRetrySkipError`. Skips silently if the file has an active request. |

## Development

Expand Down
8 changes: 4 additions & 4 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ npm start

### 方法

| 名称 | 类型 | 说明 |
| ------- | ------------------------ | -------------------- |
| `abort` | `(file: RcFile) => void` | 中止进行中的上传。 |
| `retry` | `(file: RcFile) => void` | 重试特定文件的上传。 |
| 名称 | 类型 | 说明 |
| --- | --- | --- |
| `abort` | `(file: RcFile) => void` | 中止进行中的上传。 |
| `retry` | `(file: RcFile) => void` | 重试特定文件的上传。仅对至少上传过一次(有缓存信息)的文件生效,复用首次处理的 `beforeUpload`、`action` 和 `data`。若缓存不存在,通过 `onError` 通知 `UploadRetrySkipError`;若文件已有活动请求则静默跳过。 |

## 本地开发

Expand Down
49 changes: 37 additions & 12 deletions src/AjaxUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class AjaxUploader extends Component<UploadProps> {

reqs: Record<string, any> = {};

private fileInfoCache: Map<string, ParsedFileInfo> = new Map();

private fileInput: HTMLInputElement;

private _isMounted: boolean;
Expand Down Expand Up @@ -157,6 +159,7 @@ class AjaxUploader extends Component<UploadProps> {
componentWillUnmount() {
this._isMounted = false;
this.abort();
this.fileInfoCache.clear();
document.removeEventListener('paste', this.onFilePaste);
}

Expand Down Expand Up @@ -269,6 +272,8 @@ class AjaxUploader extends Component<UploadProps> {

const { uid } = origin;

this.fileInfoCache.set(uid, { data, origin, action, parsedFile });
Comment thread
EmilyyyLiu marked this conversation as resolved.

const request = customRequest || defaultRequest;

const requestOption = {
Expand Down Expand Up @@ -297,22 +302,42 @@ class AjaxUploader extends Component<UploadProps> {
},
};

onStart(origin);
this.reqs[uid] = request(requestOption, { defaultRequest });
this.reqs[uid] = {};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
try {
onStart(origin);
} catch (e) {
delete this.reqs[uid];
throw e;
}
try {
const handle = request(requestOption, { defaultRequest });
if (this.reqs[uid]) {
this.reqs[uid] = handle || {};
}
} catch (e) {
delete this.reqs[uid];
const { onError } = this.props;
const err = e instanceof Error ? e : new Error(String(e));
onError?.(err as UploadRequestError, null, parsedFile);
}
}

retry = (originFile: RcFile) => {
const { uid } = originFile;
this.processFile(originFile, [originFile])
.then(fileInfo => {
if (this.reqs[uid]) {
return;
}
if (fileInfo.parsedFile) {
this.post(fileInfo);
}
})
.catch(() => {});
if (this.reqs[uid]) {
return;
}

const cachedFileInfo = this.fileInfoCache.get(uid);
if (!cachedFileInfo) {
const { onError } = this.props;
const err = new Error('retry skipped: file was never uploaded') as UploadRequestError;
err.name = 'UploadRetrySkipError';
onError?.(err, null, originFile);
return;
}

this.post(cachedFileInfo);
};

reset() {
Expand Down
Loading
Loading