-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module:upload): add directory support (#2164)
- Loading branch information
Showing
12 changed files
with
258 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
order: 10 | ||
title: | ||
zh-CN: 自定义上传 | ||
en-US: Custom request | ||
--- | ||
|
||
## zh-CN | ||
|
||
使用 `nzCustomRequest` 改变上传行为。 | ||
|
||
## en-US | ||
|
||
Use `nzCustomRequest` override for the default xhr behavior. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Component } from '@angular/core'; | ||
import { HttpRequest, HttpClient, HttpEventType, HttpEvent, HttpResponse } from '@angular/common/http'; | ||
import { NzMessageService, UploadXHRArgs } from 'ng-zorro-antd'; | ||
import { forkJoin } from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'nz-demo-upload-custom-request', | ||
template: ` | ||
<nz-upload | ||
nzAction="https://jsonplaceholder.typicode.com/posts/" | ||
[nzCustomRequest]="customReq"> | ||
<button nz-button> | ||
<i class="anticon anticon-upload"></i><span>Click to Upload</span> | ||
</button> | ||
</nz-upload> | ||
` | ||
}) | ||
export class NzDemoUploadCustomRequestComponent { | ||
|
||
constructor(private http: HttpClient, private msg: NzMessageService) {} | ||
|
||
customReq = (item: UploadXHRArgs) => { | ||
// 构建一个 FormData 对象,用于存储文件或其他参数 | ||
const formData = new FormData(); | ||
// tslint:disable-next-line:no-any | ||
formData.append('file', item.file as any); | ||
formData.append('id', '1000'); | ||
const req = new HttpRequest('POST', item.action, formData, { | ||
reportProgress : true, | ||
withCredentials: true | ||
}); | ||
// 始终返回一个 `Subscription` 对象,nz-upload 会在适当时机自动取消订阅 | ||
return this.http.request(req).subscribe((event: HttpEvent<{}>) => { | ||
if (event.type === HttpEventType.UploadProgress) { | ||
if (event.total > 0) { | ||
// tslint:disable-next-line:no-any | ||
(event as any).percent = event.loaded / event.total * 100; | ||
} | ||
// 处理上传进度条,必须指定 `percent` 属性来表示进度 | ||
item.onProgress(event, item.file); | ||
} else if (event instanceof HttpResponse) { | ||
// 处理成功 | ||
item.onSuccess(event.body, item.file, event); | ||
} | ||
}, (err) => { | ||
// 处理失败 | ||
item.onError(err, item.file); | ||
}); | ||
} | ||
|
||
// 一个简单的分片上传 | ||
customBigReq = (item: UploadXHRArgs) => { | ||
const size = item.file.size; | ||
const chunkSize = parseInt((size / 3) + '', 10); | ||
const maxChunk = Math.ceil(size / chunkSize); | ||
const reqs = Array(maxChunk).fill(0).map((v: {}, index: number) => { | ||
const start = index * chunkSize; | ||
let end = start + chunkSize; | ||
if (size - end < 0) { | ||
end = size; | ||
} | ||
const formData = new FormData(); | ||
formData.append('file', item.file.slice(start, end)); | ||
formData.append('start', start.toString()); | ||
formData.append('end', end.toString()); | ||
formData.append('index', index.toString()); | ||
const req = new HttpRequest('POST', item.action, formData, { | ||
withCredentials: true | ||
}); | ||
return this.http.request(req); | ||
}); | ||
return forkJoin(...reqs).subscribe(resules => { | ||
// 处理成功 | ||
item.onSuccess({}, item.file, event); | ||
}, (err) => { | ||
// 处理失败 | ||
item.onError(err, item.file); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
order: 6 | ||
title: | ||
zh-CN: 文件夹上传 | ||
en-US: Upload directory | ||
--- | ||
|
||
## zh-CN | ||
|
||
支持上传一个文件夹里的所有文件。 | ||
|
||
## en-US | ||
|
||
You can select and upload a whole directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'nz-demo-upload-directory', | ||
template: ` | ||
<nz-upload | ||
nzAction="https://jsonplaceholder.typicode.com/posts/" | ||
nzDirectory> | ||
<button nz-button> | ||
<i class="anticon anticon-upload"></i> Upload Directory | ||
</button> | ||
</nz-upload> | ||
` | ||
}) | ||
export class NzDemoUploadDirectoryComponent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
order: 6 | ||
order: 8 | ||
title: | ||
zh-CN: 图片列表样式 | ||
en-US: Pictures with list style | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
<input type="file" #file (change)="onChange($event)" | ||
[attr.accept]="options.accept" [multiple]="options.multiple" style="display: none;"> | ||
[attr.accept]="options.accept" | ||
[attr.directory]="options.directory ? 'directory': null" | ||
[attr.webkitdirectory]="options.directory ? 'webkitdirectory': null" | ||
[multiple]="options.multiple" style="display: none;"> | ||
<ng-content></ng-content> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.