0

A file is available at my server's endpoint http://xyz/file.

I want to send this file in Angular 6 via HTTP POST using multipart/form-data. Thanks to this related question, this is my approach:

    const filePath = 'http://xyz/file'
    const formData = new FormData();
    formData.append('file', filePath);

    this.http.post(endpoint, formData).subscribe(response => {
        console.log(response);
    });

The problem is, this doesn't seem to work if I specify a file path to the file's endpoint rather than the file itself. When trying this, I get an error in the console ERROR TypeError: "Argument 2 of FormData.append is not an object.".

How can I solve this? Can I somehow use the remote file for the POST? Or do I need to download the file first and then send it? How would I do that?

1 Answer 1

2

You can't send just link to a file. Formdata field expects to have the file content. You can do this in two steps

  1. Create a function to download remote file into Blob
  2. Send blob via Post request
fetch('https://some/file/here.png')
  .then(res => res.blob()) // Gets the response and returns it as a blob
  .then(blob => {
    var formData = new FormData();
    formData.append("file", blob);

    this.http.post(endpoint, formData).subscribe(response => {
        console.log(response);
    });

  });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.