Angular2使用ng2-file-upload上传文件
ng2-file-upload是一个功能比较全面的上传文件的支持库
(参考:http://www.jianshu.com/p/0741186f60ab
https://segmentfault.com/a/1190000007886391
http://valor-software.com/ng2-file-upload/)
Angular2使用ng2-file-upload上传文件:
1、安装ng2-file-upload模块:
npm install ng2-file-upload --save
2、在app.module.ts文件中,在需要的模块中引入一下模块
import { CommonModule } from '@angular/common';
import { FileUploadModule } from 'ng2-file-upload';
然后在@NgModule的imports字段中引用CommonModule和FileUploadModule。
@NgModule({
imports: [
CommonModule,
FileUploadModule
]
}
3、对应的app.component.html内容(代码有点乱,而且不知道为什么多选按钮有时弹框会很慢)
<div style="text-align:center">
<h1>
Welcome to {
{title}}!!
</h1>
</div>
<div>
<form >
<h1>多选</h1>
<input type="file" ng2FileSelect [uploader]="uploader" name="fileInput" id="fileInput" multiple />
<!--ng-submit="submit_form()"-->
<!--<input type="file" name ="filer" ng2FileSelect [uploader]="uploader" (change)="selectedFileOnChanged($event)" multiple />-->
<h1>单选</h1>
<input type="file" name ="filer" ng2FileSelect [uploader]="uploader" (change)="selectedFileOnChanged($event)" />
<table class="table bg-white-only b-a">
<tbody>
<div *ngFor="let img of