That covers the basics, but let us walk through detailed examples in this tutorial – Read on!
ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
EXAMPLE CODE DOWNLOAD
Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
METHOD 1) USE FILE SAVER
Thank goodness, someone did all the hard work. This is one of the easiest and “safest” cross-browser ways to save a file –
METHOD 2) CREATE FILE FROM BLOB & FORCE DOWNLOAD
METHOD 3) UPLOAD BLOB TO SERVER
This is an alternative to the above BLOB download – We upload the BLOB and save it on the server instead.
METHOD 4) WRITABLE FILE STREAM
Yes, the old grandmother’s age of the Internet is over. We can create a file handler and file stream on the user’s computer, use it to save a file. But this still opens a “save file as” dialog box, we cannot directly save without the user’s permission.
P.S. This will only work on Chrome, Edge, and Opera at the time of writing.
METHOD 5) WRITE TO FILE IN NODEJS
Yes, it’s that stupidly simple. Just load the file system module
Not all browsers support the BLOB data type nor all the file writing features. So it is best to do some checks before you enable your ninja features – I will recommend using Modernizr to detect if the browser supports certain features.
LINKS & REFERENCES
INFOGRAPHIC CHEAT SHEET
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!
Open your favorite browser (here we will use Google Chrome)..
Open the developer tools by right clicking on an empty area and select Inspect. Shortcut: F12 . Inspect Browser..
How create JS file in Windows?
Try NodeJS with Visual Studio Code.
Open your command line and create a new directory: mkdir HelloNode , then enter the directory: cd HelloNode..
Open the directory and your app..