如何通过ng2-file上传中的beforeonloaditem上的formdata发送表单输入值

时间:2018-04-05 13:49:33

标签: angular ng2-file-upload

组件:

    import { FileUploader } from 'ng2-file-upload/ng2-file-upload';

    const URL = 'http://localhost:4200/api/ticketsystem/createticket';

     public uploader: FileUploader = new FileUploader({ url: URL, itemAlias: 'file' });

    this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; };

    ngOnInIt(){

    this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
          console.log("ImageUpload:uploaded:", item, status, response);
        };
    this.uploader.onBeforeUploadItem = (item: any) => {
          var ll = {
            "severity": this.ticket.severity,
            "subject": this.ticket.subject,
            "description": this.ticket.description,
            "priority": "High",
            "status": "Open",
            "owner": {
              "id": this.userDetails[0].id
            }
          }

    item.formData.push({ "ticket": JSON.stringify(ll) })

        }
 }


 I have tried file upload using ng2-file-upload if i normally upload file means it can be upload but i need to send some input json object. 

上面的代码uploader.onBeforeLoadItem方法我构建了json对象我需要用uploader发送的内容。在这里我需要帮助如何使用formdata对象发送我的输入值,我需要帮助

1 个答案:

答案 0 :(得分:0)

尝试以下代码以ng2-file上载的方式发送带有文件上载的json数据:

ngOnInit() {
 this.uploader.onBuildItemForm = (fileItem: any, form: any) => {
  form.append('Field1', this.filedValue); //note comma separating key and value
  form.append('Field2', this.filedValue2);
 };
}