2

component :

    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. 

The above code uploader.onBeforeLoadItem method i have constructed json object what i need to send with uploader. Here i need help how send my input value with formdata object, I need help on this

1 Answers1

3

try below code to send json data with file upload with ng2-file upload:

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