2017-08-23 23 views
0

我在我的角度4應用程序上實現了簡單的文件上傳,一切正常,但我有一個問題,我不知道如何解決,所以當我完成上傳第一項,onSuccessItem回調觸發器,我做了一些邏輯,但當我上傳另一個文件,相同的回調不會觸發。我的問題是如何在每次上傳文件時觸發onSuccessItem回調,我不想刷新頁面。我的代碼看起來像這樣。我使用這個插件https://github.com/valor-software/ng2-file-upload已完成回調的角度4文件上傳

import { Component } from '@angular/core'; 
import {FileUploader, FileItem, ParsedResponseHeaders} from "ng2-file-upload"; 

@Component({ 
    selector: 'upload-file', 
    template: ` 
    <input type="file" ng2FileSelect [uploader]="uploader">  
    `, 
}) 
export class UploadFileComponent { 
    uploader:FileUploader; 
    ngOnInit(): void { 
     this.uploader = new FileUploader({ 
      url: 'http://url.to/upload', 
      headers: [{name:'Accept', value:'application/json'}], 
      autoUpload: true, 
     }); 
     this.uploader.onErrorItem = (item, response, status, headers) => this.onErrorItem(item, response, status, headers); 
     this.uploader.onSuccessItem = (item, response, status, headers) => this.onSuccessItem(item, response, status, headers); 
    } 

    onSuccessItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any { 
     //this gets triggered only once when first file is uploaded 
    } 

    onErrorItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any { 
     let error = JSON.parse(response); //error server response 
    } 
} 
+1

請刪除不當的標籤'angularjs' –

+0

文件上傳 - http://www.angulartutorial.net/2018/01/file-upload-and-send-data-to -backend.html – Prashobh

回答