JavaScript Answers

How to upload a blob with JavaScript?

Spread the love

To upload a Blob (Binary Large Object) with JavaScript, you typically use the XMLHttpRequest or the newer Fetch API to send the Blob data to a server-side endpoint.

Here’s a basic example using the Fetch API:

// Assume 'blob' is your Blob object

// Create a FormData object and append the Blob
const formData = new FormData();
formData.append('file', blob, 'filename.txt');

// Make a POST request with Fetch API
fetch('', {
    method: 'POST',
    body: formData
.then(response => {
    if (!response.ok) {
        throw new Error('Upload failed');
    return response.text();
.then(data => {
    console.log('Upload successful:', data);
.catch(error => {
    console.error('Error uploading file:', error);

In this example, we create a FormData object and append your Blob to it. You can also include additional form data if needed.

Then, you use the Fetch API to make a POST request to the server’s endpoint ( in this case).

In the Fetch options, you specify the method as ‘POST’ and set the body of the request to the FormData object containing your Blob data.

We handle the response in the Promise chain. If the response is successful (status code 200-299), you can handle the returned data. Otherwise, you handle the error.

Make sure to replace '' with the actual URL of your server-side endpoint where you want to upload the Blob data.

On the server-side, you would handle the file upload according to your server environment (e.g., Node.js, PHP, Python, etc.).

The uploaded Blob data will typically be available in the request body or as a file on the server, depending on how you handle the upload.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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