How to Create and Save File in JavaScript
- Using the Blob and URL.createObjectURL Method
- Using the FileSaver.js Library
- Saving JSON Data to a File
- Conclusion
- FAQ

Creating and saving files in JavaScript is a fundamental skill for web developers, enabling them to enhance user experiences by allowing file downloads, data storage, and more. Whether you’re building a web application that requires user-generated content or simply want to save data locally, understanding how to create and save files is essential.
In this tutorial, we’ll dive into the different methods to achieve this in JavaScript, focusing on practical examples and clear explanations. By the end of this guide, you’ll have the knowledge to create and save files effortlessly, making your web applications more dynamic and functional.
Using the Blob and URL.createObjectURL Method
One of the most straightforward ways to create and save a file in JavaScript is by using the Blob object along with the URL.createObjectURL method. This method allows you to create a file-like object of immutable, raw data. It’s particularly useful for generating files dynamically, such as text files or images.
Here’s an example of how to create and save a text file:
const data = "Hello, World!";
const blob = new Blob([data], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'hello.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
In this code, we start by defining the content we want to save, which is a simple string “Hello, World!”. We then create a Blob object that contains this data, specifying the MIME type as ’text/plain’. Next, we generate a URL for the Blob using URL.createObjectURL, which we assign to an anchor element’s href attribute. By setting the download attribute, we indicate the filename the user should see when downloading. The anchor is then programmatically clicked to trigger the download, and we clean up by removing the anchor and revoking the URL.
Using the FileSaver.js Library
For more complex file-saving needs, the FileSaver.js library is a powerful tool. It simplifies the process of saving files in various formats and is compatible with multiple browsers. This library is particularly useful when dealing with larger files or when you need more control over the file-saving process.
To use FileSaver.js, you first need to include the library in your project. You can do this by adding the following script tag in your HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
Once you have included the library, here’s how you can create and save a text file:
const data = new Blob(["Hello, World!"], { type: 'text/plain' });
saveAs(data, 'hello.txt');
In this example, we create a Blob object just as we did previously, but instead of manually creating an anchor element, we use the saveAs
function provided by FileSaver.js. This function takes care of the download process for us, making it more straightforward and reliable across different browsers. The result is the same: a text file named “hello.txt” is downloaded, containing the string “Hello, World!”.
Saving JSON Data to a File
Saving JSON data is a common requirement in web applications, especially when dealing with APIs or user-generated content. The process is similar to saving plain text files, but we need to convert our data into a JSON string first.
Here’s how to save a JSON object as a file:
const jsonData = { name: "John", age: 30, city: "New York" };
const blob = new Blob([JSON.stringify(jsonData, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'data.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
In this code, we define a JSON object and convert it into a string using JSON.stringify()
. The second parameter of JSON.stringify
is used to format the output for better readability. We then create a Blob object with the JSON string and specify the MIME type as ‘application/json’. The process of creating a download link and triggering the download is the same as in the previous examples. The result is a file named “data.json” that contains the user data in JSON format.
Conclusion
Creating and saving files in JavaScript is a vital skill for developers looking to enhance their web applications. Whether you’re generating simple text files, using libraries like FileSaver.js for more complex needs, or saving JSON data, the methods we’ve explored provide a solid foundation for file handling in JavaScript. By mastering these techniques, you can improve user interactions and create more dynamic applications. Now that you have the knowledge, you can experiment with these methods and see how they can be applied to your projects.
FAQ
-
How can I create a file in JavaScript?
You can create a file in JavaScript using the Blob object and URL.createObjectURL method, or by using libraries like FileSaver.js. -
What types of files can I save using JavaScript?
You can save various types of files, including text files, JSON files, images, and more, depending on the data you generate. -
Is it necessary to use a library for saving files in JavaScript?
No, it’s not necessary. You can save files using native JavaScript methods, but libraries like FileSaver.js can simplify the process and improve compatibility.
-
Can I save files directly to the user’s computer?
JavaScript running in the browser cannot directly save files to the user’s computer without user interaction. The typical method involves prompting the user to download the file. -
What is the MIME type, and why is it important?
MIME type indicates the nature and format of a document. It’s important because it helps the browser understand how to handle the file being downloaded.
Harshit Jindal has done his Bachelors in Computer Science Engineering(2021) from DTU. He has always been a problem solver and now turned that into his profession. Currently working at M365 Cloud Security team(Torus) on Cloud Security Services and Datacenter Buildout Automation.
LinkedIn