Skip to content

WebNinjaDeveloper.com

Programming Tutorials




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

React.js react-download-link Example to Download Raw Text & CSV Files as Attachment in Browser Using Javascript

Posted on February 15, 2023

 

 

Welcome folks today in this blog post we will be downloading raw text and csv files as attachment in browser using react-download-link library in browser using javascript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new react.js project using the below command

 

 

npx create-react-app sampleapp

 

 

cd sampleapp

 

 

And after that you will see the below directory structure of the react.js app as shown below

 

 

 

 

And now we need to edit the App.js file and copy paste the following code

 

 

App.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
import DownloadLink from "react-download-link";
 
export default function App() {
  return (
    <div className="App">
      <DownloadLink
        label="Save"
        filename="myfile.txt"
        exportFile={() => "My cached data"}
      />
    </div>
  );
}

 

 

As you can see we are importing the react-download-link library at the top and then we are using the DownloadLink element where we are passing the label and filename and then we have the callback function where we providing the data which will be present inside the text file.

 

 

 

 

 

Downloading CSV File

 

 

Now we will be providing the .csv extension inside the filename and then we will be providing the csv data inside the export method as shown below

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
import DownloadLink from "react-download-link";
 
export default function App() {
  return (
    <div className="App">
      <DownloadLink
        label="Save"
        filename="myfile.csv"
        exportFile={() => "Name,Age,Couuntry\nJohn,25,India"}
      />
    </div>
  );
}

 

 

 

Recent Posts

  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Android Java Project to Detect System Hardware & System CPU Info & Display inside TextView Widget
  • Android Java Project to Integrate Google OAuth2 Login & Logout System & Save User Info in SharedPreferences
  • Android Java Project to Export Raw Text to PDF Document Using iTextPDF Library
  • Android Java Project to Export Images From Gallery to PDF Document Using iTextPDF 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