import { FileUploader } from 'ng2-file-upload';
this.uploader = new FileUploader({
url: this.importExcelUrl,
method: 'POST',
itemAlias: 'excel',
});
// 選擇的文件
selectedFileOnChanged (item) {
console.log(item);
}
// 上傳文件,這裏是全部上傳
fileAllUp () {
this.uploader.uploadAll();
this.uploader.queue[0].onSuccess = (response, status, headers) => {
// 上傳文件成功
if (response['status'] == 'ok') {
this.dataTips = '上傳成功 ! -----' + response['message'];
}else {
this.dataTips = '上傳失敗 ! -----' + response['message'];
}
}
}
// 清空選擇列表
fileAllDelete (): any {
this.uploader.clearQueue();
}
<input type="file" id="fileInput" ng2FileSelect [uploader]="uploader" (change)="selectedFileOnChanged($event)" multiple />
<h5>文件數量: {{ uploader?.queue?.length }}</h5>
<table>
<thead>
<tr>
<th>名稱</th>
<th>大小</th>
<th>狀態</th>
<th>操作</th>
</tr>
</thead>
<tbody >
<tr *ngFor="let item of uploader.queue">
<td><strong>{{ item?.file?.name }}</strong></td> <!--文件名-->
<td><strong>{{ item?.file?.size/1024/1024 | number:'.2' }} MB</strong></td>
<td class="tableth">
<span *ngIf="item.isSuccess">成功</span>
<span *ngIf="!item.isUploaded">未上傳</span>
<span *ngIf="item.isError">錯誤</span>
</td>
<td>
<button class="btn btn-danger btn-sm" (click)="item.remove()">刪除</button>
</td>
</tr>
</tbody>
</table>
<button class="btn btn-primary" (click)="fileAllUp()" >全部上傳</button>
<button class="btn btn-danger" (click)="fileAllDelete()" >清除列表</button>
headers -----> 上傳文件的請求頭參數
method -----> 上傳文件的方式
maxFileSize -----> 最大可上傳文件的大小
url -----> 地址
itemAlias -----> 文件別名
progress -----> 上傳進度
cancel() -----> 取消上傳
upload() -----> 上傳文件
附加參數
1 additionalParameter: {}
2 this.ouloader.setOptions( {} )