angular2使用ng2-uploader上传文件

时间:2017-06-21 05:08:42

标签: angular

我是angular2的新手。我正在使用ng2-uploader上传文件。 我正在使用下面的代码:

   <input type="file" ngFileSelect [options]="options" (onUpload)="handleUpload($event)" (beforeUpload)="beforeUpload($event)">

在组件中:

    uploadFile: any;
    hasBaseDropZoneOver: boolean = false;
    options: Object = {
    url: 'http://localhost:3000/upload'
    };       

在module.ts中:

   import { Ng2UploaderModule } from 'ng2-uploader';

2 个答案:

答案 0 :(得分:0)

您是否在组件中实施了 handleUpload beforeUpload 像这样:

 handleUpload(data): void {
     if (data && data.response) {
         data = JSON.parse(data.response);
         this.uploadFile = data;
     }
 }

fileOverBase(e:any):void {
    this.hasBaseDropZoneOver = e;
}

beforeUpload(uploadingFile): void {
    if (uploadingFile.size > this.sizeLimit) {
      uploadingFile.setAbort();
      alert('File is too large');
    }
}

要获得完整的代码,请浏览following link并查看是否遗漏了任何内容

答案 1 :(得分:0)

尝试使用这些选项。

 options: object = {
 url: 'http://localhost://sitename/api/uploader.php',
    filterExtensions: false,
   allowedExtensions: ['jpg', 'png']
  };