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 :: double click on element using javascript 
Javascript :: js promise api 
Javascript :: react native build 
Javascript :: angular create injectable 
Javascript :: trash alternate outline icon not coming to right side react 
Javascript :: text input underline react native 
Javascript :: creating a json 
Javascript :: syntax of reduce in js 
Javascript :: bresenham algorithm 
Javascript :: how to make javascript function consise 
Javascript :: unregister react hook form 
Javascript :: map in js 
Javascript :: js contenteditable button spacebar 
Javascript :: javascript function with string parameter 
Javascript :: multiply js 
Javascript :: create a regex javascript 
Javascript :: find second smallest number in array javascript using for loop 
Javascript :: Different views for Desktop and mobile Angular 
Javascript :: javascript index of biggest number 
Javascript :: JavaScript Code to Perform GCD using Recursion 
Javascript :: JQuery Autocomplete no result found 
Javascript :: mongoose autoincrement 
Javascript :: django pointfield json example 
Javascript :: WebPack Multiple files 
Javascript :: mongoose search multiple fields 
Javascript :: javascript and json 
Javascript :: webpack dev srcipt 
Javascript :: javascript extract json from string 
Javascript :: scrape html table javascript 
Javascript :: hide playback speed from videojs 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =