-1

I´m trying to send input file to server. When input file has data return in console this

 FileList {0: File, length: 1}0: File {name: "8mn4p369li331.png", lastModified: 1567609045831, lastModifiedDate: Wed Sep 04 2019 16:57:25 GMT+0200 (hora de verano de Europa central), webkitRelativePath: "", size: 9587062, …}length: 1__proto__: FileList

I try to put this in formData element like this but doesn´t work

  save(form: NgForm) {
       const formData: FormData = new FormData();
       formData.append('file', this.selectedFiles, this.fileName)
       form.value["file"] = formData;
}

So: What´s is the right way to upload file (in a form) to the server?

UPDATE: onChange function

  fileEvent(event:any){
    this.selectedFiles = event.target.files;
    this.fileName = this.selectedFiles[0].name;
    console.log(this.selectedFiles);

}
2
  • You updated your comment, but what is the conclusion? Perhaps you should create a minimal setup? Commented Oct 18, 2019 at 11:52
  • 1
    see this link that is complete :stackoverflow.com/questions/57497240/… Commented Oct 18, 2019 at 11:57

2 Answers 2

0

Provided you have the right file:

let formdata = new FormData();
formdata.set('file', file, "fileName");

I believe you can only set one file per variable...

Edit: My file variable is a Blob...

Blob {size: 3151, type: "application/pdf"}
Sign up to request clarification or add additional context in comments.

2 Comments

Failed to execute 'set' on 'FormData': parameter 2 is not of type 'Blob'. The parametmer 2 is this.selectedFiles[0].name;
Do you need help with getting a blob?
0

your save function should be

save(form: NgForm) {
   const formData: FormData = new FormData();
   formData.append('file', this.selectedFiles[0], this.fileName)
   form.value["file"] = formData;
}

3 Comments

Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'.
second parameter should be this.selectedFiles.files[0]
formData.set('file', this.selectedFiles, this.fileName); form.value["file"] = formData.get("file"); Works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.