How can JavaScript upload a blob ?
Last Updated :
29 Jul, 2024
There are many ways to upload a blob (a group of bytes that holds the data stored in a file) in JavaScript, using XMLHttpRequest, Fetch API, jQuery. In this tutorial, we will discuss the two most common ways that are supported by a majority of the browsers.
Note: To test our HTTP request, you can use minimal express server. You can also use any online testing tool like webhook.site.
Fetch API: The Fetch API allows web browsers to send and receive data from the servers through HTTP requests. It is an easy-to-use version of XMLHttpRequest. The fetch() method returns a Promise which then can be chained with then() and catch() for better error handling. It is supported by all the modern browsers except IE.
Syntax:
fetch( url, // API endpoint
{
method:"", // HTTP Method (GET, POST, PUT, DELETE)
body: {}, // Data to be sent
}).then(response => {
// handle the response
})
.catch(e => {
// handle the error
});
Example:
JavaScript
<script>
function uploadBlob() {
const blob =
new Blob(
["This is some important text"],
{ type: "text/plain" }
);
// Creating a new blob
// Hostname and port of the local server
fetch('http://localhost:3000', {
// HTTP request type
method: "POST",
// Sending our blob with our request
body: blob
})
.then(response => alert('Blob Uploaded'))
.catch(err => alert(err));
}
document.addEventListener('load', uploadBlob())
</script>
Output:
AJAX: It is a set of web applications that can send and retrieve data from a server asynchronously without reloading the current page. Please import or load jQuery before calling the ajax function.
Syntax:
$.ajax({
url: "", // API Endpoint
type: "", // HTTP Method (GET, POST, PUT, DELETE)
data: {}, // Data to be sent
// Specifies how the form-data should be encoded
enctype: 'multipart/form-data',
success: function(data) {
// Handle the response
}, error: function(e) {
// Handle the error
}
});
Example:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<script src=
"https://code.jquery.com/jquery-3.6.0.min.js">
</script>
</head>
<body>
<script>
function uploadBlob() {
const blob = new Blob(
["This is some important text"],
{ type: "text/plain" }
);
// Creating a new blob
$.ajax({
// Hostname and port of the local server
url: "http://localhost:3000/",
// HTTP request type
type: "POST",
// Sending blob with our request
data: blob,
processData: false,
contentType: false,
success: function (data) {
alert('Blob Uploaded')
},
error: function (e) {
alert(e);
}
});
}
$(document).on('load', uploadBlob());
</script>
</body>
</html>
Output:
Similar Reads
How to Upload Files in JavaScript? We upload files using JavaScript, for this we have to capture form elements, gather information from FormData for easier file uploads, intercept submission events, and utilize Fetch API for asynchronous server requests, for enhanced user experience and efficient data handling. ApproachProject Setup:
1 min read
What is a Blob Object in JavaScript ? In JavaScript, a Blob (Binary Large Object) is an object that represents raw binary data(collection of bytes). It is commonly used to handle and manipulate binary data, such as images, audio, video, or other types of files. The Blob object allows you to create, modify, and manipulate binary data in
2 min read
How to Convert Base64 to Blob in JavaScript? Working with files and data in web applications often involves dealing with binary data. One common scenario is converting a Base64 string into a Blob object, which can then be used in various ways, such as creating downloadable files or uploading images to a server. This article will guide you thro
4 min read
How to Convert JSON to Blob in JavaScript ? This article explores how to convert a JavaScript Object Notation (JSON) object into a Blob object in JavaScript. Blobs represent raw data, similar to files, and can be useful for various tasks like downloading or processing JSON data. What is JSON and Blob?JSON (JavaScript Object Notation): A light
2 min read
JavaScript Blob A Blob object represents a collection of binary data stored as a file. Unlike a mere reference to a file, a blob possesses its own size and MIME type, similar to regular files. Depending on the browser's capabilities and the blob's size, this data can be stored either in the memory or filesystem of
3 min read