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

download file in react

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

PREVIOUS NEXT
Code Example
Javascript :: select react hook form yup validation 
Javascript :: push data to firebase javascript 
Javascript :: how to run javascript in chrome 
Javascript :: Capitalize the first letter of string using JavaScript 
Javascript :: file upload javascript 
Javascript :: react native only 1 corner rounded 
Javascript :: can promise is going to be handle asynchronously 
Javascript :: javascript get last object in foreach loop 
Javascript :: add property to all documents mongo 
Javascript :: post jquery 
Javascript :: mongodb replace string regex 
Javascript :: js largura da tela 
Javascript :: npm run js file from command line 
Javascript :: how to get all the voice channels in discord js 
Javascript :: normalize method javascript 
Javascript :: react port 
Javascript :: remove .html from url express js 
Javascript :: add checkbox dynamically in javascript 
Javascript :: js stairs algorithm 
Javascript :: useReducer 
Javascript :: how to unselect input javascript 
Javascript :: get current file name javascript 
Javascript :: how to see node taints 
Javascript :: parsley js decimal 
Javascript :: push json into json 
Javascript :: get specific parent element jquery 
Javascript :: jquery datatime 
Javascript :: jquery import js file 
Javascript :: js check if array is empty 
Javascript :: all &nbsp; to space from string javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =