2

零件 :

    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 对象。在这里我需要帮助如何使用 formdata 对象发送我的输入值,我需要帮助

4

1 回答 1

2

尝试使用以下代码通过 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);
 };
}
于 2018-08-29T09:14:11.777 回答