Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react js download file

fetch('https://cors-anywhere.herokuapp.com/' + fileURL, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/pdf',
    },
  })
  .then((response) => response.blob())
  .then((blob) => {
    // Create blob link to download
    const url = window.URL.createObjectURL(
      new Blob([blob]),
    );
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute(
      'download',
      `FileName.pdf`,
    );

    // Append to html link element page
    document.body.appendChild(link);

    // Start download
    link.click();

    // Clean up and remove the link
    link.parentNode.removeChild(link);
  });
Comment

how to download file from link in react

import axios from 'axios'
import fileDownload from 'js-file-download'
 
...

handleDownload = (url, filename) => {
  axios.get(url, {
    responseType: 'blob',
  })
  .then((res) => {
    fileDownload(res.data, filename)
  })
}
 
...

<button onClick={() => {this.handleDownload('https://your-website.com/your-image.jpg', 'test-download.jpg')
}}>Download Image</button>
Comment

Make a file downloadable in React

import React from "react";
import { saveAs } from "file-saver";

export default function App() {
  const saveFile = () => {
    saveAs(
      "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
      "example.pdf"
    );
  };
  return (
    <div>
      <button onClick={saveFile}>download</button>
    </div>
  );
}
Comment

download file in react

var fileDownload = require('js-file-download');
fileDownload(data, 'filename.csv');
Comment

PREVIOUS NEXT
Code Example
Javascript :: js filter to remove empty string in array. 
Javascript :: set timeout JS for loop 
Javascript :: app use morgan 
Javascript :: js custom event 
Javascript :: recursion javascript 
Javascript :: how to get the div value in jquery 
Javascript :: react native images 
Javascript :: ajax open new tab with post 
Javascript :: chartjs begin at 0 
Javascript :: object json parse javascript 
Javascript :: javascript on window resize 
Javascript :: Use Destructuring Assignment with the Rest Operator to Reassign Array Elements 
Javascript :: if else js 
Javascript :: ajax current url 
Javascript :: buffer in javascript 
Javascript :: node js get list of all names of object array 
Javascript :: get current time in different timezone javascript 
Javascript :: js capitalize first letter of each word 
Javascript :: base64 to blob 
Javascript :: remove duplicate elements array javascript 
Javascript :: js function string parameter 
Javascript :: while vs do while javascript 
Javascript :: useeffect only on mount 
Javascript :: image upload react 
Javascript :: javascript returning a function 
Javascript :: new line javascript 
Javascript :: javascript check if two arrays contain same values 
Javascript :: javascript random number up to including 2 
Javascript :: canactivate angular 
Javascript :: javascript creeate utc date 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =