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( {} )