
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How can JavaScript upload a blob?
Blob stands for Binary Large Object and they are used to store binary data like images, audio or other multimedia objects, and sometimes binary executable code is also stored as a blob.
We can use JavaScript to upload blob like any other data file.
JavaScript can upload a Blob using the XMLHttpRequest or fetch API.
1. Using XMLHTTPRequest
XMLHttpRequest (XHR) is an API in the form of an object whose methods transfer data between a web browser and a web server. The JavaScript environment of the browser provides the object. It is usually used to send and receive data asynchronously without restarting the website. This makes it possible to enjoy a dynamic, user-friendly, and speedy web page.
Example
Here is an example of uploading a Blob using XMLHttpRequest −
var blob = new Blob(["Some conventional data"], { type: "text/plain" }); var xhr = new XMLHttpRequest(); xhr.open("POST", "/upload", true); xhr.onload = function (e) { if (xhr.readyState === 4 && xhr.status === 200) { console.log(xhr.responseText); } }; xhr.send(blob);
Here we are making a POST request to the /upload endpoint of our backend API with some blob data. And on the successful response of the server, we are logging out the response.
2. Using Fetch API
The Fetch API allows you to make requests to servers and retrieve data from them. It is built into modern web browsers and can be used to make both GET and POST requests. The Fetch API uses the fetch() method, which returns a promise that resolves to a Response object. This Response object can then be used to access the data returned by the server. The Fetch API is often used as an alternative to the older XMLHttpRequest API and is more modern and user-friendly. It is also more versatile, as it can be used to make requests to servers other than the one the webpage is hosted on.
Here is an example of uploading a Blob using fetch −
var blob = new Blob(["Some conventional data"], { type: "text/plain" }); var formData = new FormData(); formData.append("file", blob); fetch("/upload", { method: "POST", body: formData, }) .then((response) => response.text()) .then((responseText) => { console.log(responseText); });
Therefore, in this way, you can either use the XMLHTTPRequest or fetch API to upload blob data to the server from the frontend vanilla JavaScript without using any third party library.
- Related Articles
- How can you upload a file using JSP?
- Blob object in JavaScript
- How can I upload a Google doc from SAP
- How to convert an Image to blob using JavaScript?
- How to upload file without form using JavaScript?
- How can we retrieve a blob datatype from a table using the getBinaryStream() method in JDBC?
- Anyone Can Upload Files In Your Dropbox
- How can we upload data into MySQL tables by using mysqlimport?
- How to upload an image using HTML and JavaScript in Firebase?
- How to upload a file using JSP?
- How to upload a file in Cypress?
- How can we upload data into multiple MySQL tables by using mysqlimport?
- How to upload a 360 video to YouTube?
- How to preview an image before and after upload in HTML and JavaScript?
- How to create a file upload button with HTML?
