如何使用ng2-file-upload返回响应 [英] How to return response with ng2-file-upload
本文介绍了如何使用ng2-file-upload返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Angular 4中使用ng2-file-upload.有什么方法可以从服务器返回自定义数据吗?我们如何在ts文件中访问它?
I am using ng2-file-upload in angular 4. Is there any way to return custom data from server? How would we access it in ts file?
推荐答案
您应该使用上载程序onSuccessItem和onErrorItem回调:
You should be using uploader onSuccessItem and onErrorItem callbacks:
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 {
let data = JSON.parse(response); //success server response
}
onErrorItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any {
let error = JSON.parse(response); //error server response
}
}
这篇关于如何使用ng2-file-upload返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文