完成回调时上传角度4文件

时间:2017-08-23 18:28:14

标签: angular typescript ng-file-upload ng2-file-upload

我在我的angular 4应用程序上实现了简单的文件上传,一切运行良好,但我有一个问题,我不确定如何修复,所以当我完成上传第一项,onSuccessItem回调触发器,我做那里有一些逻辑,但是当我上传另一个文件时,同一个回调不会触发。我的问题是如何在每次上传文件时触发onSuccessItem回调,我不想刷新页面。我的代码看起来像这样。我正在使用此插件https://github.com/valor-software/ng2-file-upload

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 个答案:

答案 0 :(得分:1)

看起来像是图书馆中的一个错误。

此处报告:https://github.com/valor-software/ng2-file-upload/issues/872由您