How can JavaScript upload a blob ? Last Updated : 29 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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: Comment More infoAdvertise with us Next Article How can JavaScript upload a blob ? ahampriyanshu Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions 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 How to Convert Blob Data to JSON in JavaScript ? When dealing with Blob data in JavaScript, such as binary data or files, we may need to convert it into JSON format for doing so JavaScript provides us with various methods as listed below. Table of Content Using FileReader APIUsing TextDecoder APIUsing FileReader APIIn this approach, we first use t 2 min read How to upload a file in PHP ? In this article, we will learn how to upload a file using PHP. Let us first understand some basic configurations. Approach: In your "php.ini" file, search for the "file_uploads" parameter and set it to "On" as mentioned below. file_uploads = On In the "index.html" file, the enctype must be multipart 3 min read How to convert blob to base64 encoding using JavaScript ? Blob in JavaScript is a fundamental data type representing bytes of data. It serves as the underlying structure for files and is supported by web browsers, capable of storing and retrieving data from memory. Blobs have specific sizes and file types like regular files and can be handled with Buffers, 2 min read How to upload file without form using JavaScript ? There are several approaches to upload a file without using the form in JavaScript: Approach 1: This approach is to use FormData that can upload a file without using any kind of form. The special thing about this is that network methods, such as fetch, can accept a FormData object as a body. Itâs en 2 min read How to read a local text file using JavaScript? Reading a local text file involves accessing the contents of a file stored on a user's device. Using JavaScript, this can be achieved with the HTML5 File API and the FileReader object, which reads files selected through an <input> element or drag-and-drop, asynchronously. Getting Started with 4 min read Like