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-sweet-progress Example to Download File Using Fetch With ProgressBar Animation in Browser Using Javascript

Posted on February 16, 2023

 

 

Welcome folks today in this blog post we will be using the react-sweet-progress library to download file from url using fetch library with progressbar animation 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 app using the below command as shown below

 

 

npx create-react-app sampleapp

 

 

cd sampleapp

 

 

And after that you need to install the below library using the below command as shown below

 

 

npm i react-sweet-progress

 

 

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

 

 

 

 

 

Showing Simple Progressbar

 

 

Now we can modify 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
14
15
16
17
import "./App.css";
import { Progress } from "react-sweet-progress";
import "react-sweet-progress/lib/style.css";
 
function App() {
 
  return (
    <div>
      <Progress />
      <Progress percent={88} status="success" />
      <Progress percent={43} status="error" />
      <Progress type="circle" percent={100} status="success" />
    </div>
  );
}
 
export default App;

 

 

As you can see we are importing the react-sweet-progress and then we are also importing the style sheet file for this progressbar. And then inside this we are rendering the progressbar widget and then we are passing the percent and status attributes to the progressbar.

 

 

 

 

Downloading File With Progressbar

 

 

Now guys we will be downloading file using fetch api with progressbar animation as shown below.

 

 

App.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import "./App.css";
import { Progress } from "react-sweet-progress";
import "react-sweet-progress/lib/style.css";
import { useState } from "react";
 
function App() {
  const [progress, setProgress] = useState(0);
 
  const handleDownload = async () => {
    let progress = 0;
    const intervalId = setInterval(async () => {
      progress += 5;
      if (progress > 100) {
        clearInterval(intervalId);
        const response = await fetch("https://images.unsplash.com/photo-1676481945425-621e73a056ce?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwyfHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=500&q=60", {
          method: "GET",
          headers: {
            "Content-Type": "application/json",
          }
        });
        const blob = await response.blob();
        const url = URL.createObjectURL(blob);
        const link = document.createElement("a");
        link.href = url;
        link.download = "sample.jpg";
        link.click();
        setProgress(0)
      } else {
        setProgress(progress);
      }
    }, 500);
  };
 
  return (
    <div>
      <button onClick={handleDownload}>Download File</button>
      <Progress percent={progress} status="active" />
    </div>
  );
}
 
export default App;

 

 

As you can see we are having the progress hooks variable for tracking the percent of the progress downloaded file. And then we have the button in which we have binded the onClick where we are executing the handleDownload() method and inside it we are using the fetch api to download the file as an attachment using the blob property.

 

 

 

 

 

Similarly we can even have the circular progressbar by providing the type property equal to circle as shown below

 

 

JavaScript
1
2
3
4
5
6
return (
    <div>
      <button onClick={handleDownload}>Download File</button>
      <Progress type="circle" percent={progress} status="active" />
    </div>
  );

 

 

 

 

Recent Posts

  • 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
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Android Java Project to Download Random Image From Unsplash Using OkHttp & Picasso Library & Display it
  • 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