2017-06-21 64 views
0

我是angular2中的新成員。我正在使用ng2-uploader上傳文件。 我html的我用下面的代碼: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'; 

回答

0

你有沒有實現的handleUploadbeforeUpload在組件 類似這樣的:

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,並檢查是否缺少任何東西

+0

我遵循相同的鏈接和使用函數「handleUpload」和「fileOverBase」和「beforeUpload」。它顯示正確的錯誤,我上傳超過文件大小,但我的文件大小是好的比它顯示我的錯誤:「錯誤TypeError:this.files.filter不是在NgFileSelectDirective.onChange(ng-file-select.ts:70 )「.... @ Ozesh – rohit13807

0

嘗試使用這些選項。

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