Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

How to Create Text File & Download File From Server Using Javascript | Javascript File Download Example For Beginners

Posted on November 19, 2022

 

 

Welcome folks today in this blog post we will be creating the text file and download it from the server using javascript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make an index.html file and copy paste the following code

 

 

index.html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <textarea name="" id="textarea" cols="30" rows="10"></textarea>
    <button id="button">Download File</button>
</body>
</html>

 

 

As you can see we are creating the textarea element and inside it we have given the id parameter to the textarea so that we can get it’s value in javascript. And also we have the button to create and download the text file.

 

 

And now we need to write the javascript code for this above app as shown below

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
let button = document.getElementById('button')
 
    button.addEventListener('click',(e) => {
        let text = document.getElementById('textarea').value
 
        let filename = "filename.txt"
 
        download(text,filename)
 
    })

 

 

As you can see in the above code we are targeting the button using it’s id and then we are binding the event listener of click. So when we click the button what should happen. So here in this we are first of all fetching the value of the textarea and then we are giving the output filename and then we are calling the download() method where  we are passing the two arguments which is the actual text and the filename. And now we need to define this function which is download() method as shown below

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function download(text,filename){
        let element = document.createElement('a')
 
        element.setAttribute('href','data:text/plain;charset=utf-8,' + encodeURI(text))
 
        element.setAttribute('download',filename)
 
        element.style.display = "none"
 
        document.body.appendChild(element)
 
        element.click()
 
        document.body.removeChild(element)
    }

 

 

As you can see inside this method we are first of creating the anchor element and then inside it we are setting the attribute of href and inside it we are using the encodeURI() method to convert the entered text to an encoded text. And then we are setting the download attribute. And then we are clicking the anchor element automatically. And after that we are removing the anchor element automatically.

 

 

Recent Posts

  • Android Java Project to Download Multiple Images From URL With Progressbar & Save it inside Gallery
  • Android Java Project to Capture Image From Camera & Save it inside Gallery
  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme