How to upload a blob with JavaScript?

Spread the love

Sometimes, we want to upload a blob with JavaScript.

In this article, we’ll look at how to upload a blob with JavaScript.

How to upload a blob with JavaScript?

To upload a blob with JavaScript, we call fetch.

For instance, we write

const response = await fetch(`https://example.com/upload.php`, {
  method: "POST",
  body: blobData,
});
const text = await response.text();

to call fetch with the URL we want to make the request to and an object that has the request method and body

We set the method to 'POST' to make a post request.

Then we set body to the blobData Blob instance to use that as the request body.

Finally, we get the response from the returned promise.

And we call text to get the response body text in a promise.

Conclusion

To upload a blob with JavaScript, we call fetch.

Leave a Reply

Your email address will not be published. Required fields are marked *