11

I know how to download a file when clicking a button using html

<a href="./abcf32x.pdf" download="How-to-download-file.pdf">
    <button>Download File</button>
</a>

But using Material-UI component how can I do this

I have the following component

<div>
  <Button
    variant="contained"
    color="#ffa726"
    size="large"
    startIcon={<GetAppIcon />}
  >
    Download Sample Method File
  </Button>
</div>

enter image description here

Now I want to download a file whose url is http://localhost:8000/static/sample_method.py

I don't want to open the link in browser and then do save as, rather it should get downloaded directly.

0

8 Answers 8

8

You already had your answer in the question. Instead of declaring a element with an href and download attribute using JSX syntax. Create that a element and click it programmatically:

function App() {
  const onDownload = () => {
    const link = document.createElement("a");
    link.download = `download.txt`;
    link.href = "./download.txt";
    link.click();
  };

  return (
    <Button onClick={onDownload} variant="contained" color="primary">
      Download
    </Button>
  );
}

Live Demo

Edit 66811401/reactjs-material-ui-how-to-download-a-file-on-clicking-a-button

Sign up to request clarification or add additional context in comments.

9 Comments

this only works when the content-description has attachment
Do you know why this is not working? stackoverflow.com/questions/68666135/… @NearHuscarl
@325 if you're using CRA, try putting the asset (the file to be downloaded) in the public folder
What is CRA? @NearHuscarl
import React from "react"; import Button from "@material-ui/core/Button"; export default function DownloadFile() { const onDownload = () => { const link = document.createElement("a"); link.download = `testDownload.txt`; link.href = "./files/testDownload.txt"; link.click(); }; return ( <Button onClick={onDownload} size="small" color="primary"> Download </Button> ); } @NearHuscarl
|
4

According to the docs, I thinks this will work, and in case of using nextjs, don't forget to put that file inside the public folder

<Button variant='contained' component="label">
    <a href="/files/CV.pdf" target="_blank" download>
         Download csv
    </a>
</Button>

Comments

3

You can use the inbuilt href prop along with the download prop of MUI Button like this:

<Button
variant="contained"
color="info"
endIcon={<FiDownload />}
href="/resume.pdf"
download
>
 Resume
</Button>

Comments

0

You can also use the

file-saver

package to download your file.

To install it, run:

npm i file-saver

Then you can call the saveAs function from the package by writing:

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>
  );
}

The first argument is the URL to download and the 2nd argument is the file name of the downloaded file.

Comments

0

Just add the download parameter to the Button element as in the example below:

<div>
  <Button
    variant="contained"
    color="#ffa726"
    size="large"
    startIcon={<GetAppIcon />}
    download
  >
    Download Sample Method File
  </Button>
</div>

Comments

0

If file is uploaded from UI then we have to download in the following way

uploaded file:

const data = {
  lastModified: 1701069121191,
  lastModifiedDate: Mon Nov 27 2023 12:42:01 GMT+0530 (India Standard Time) {},
  name: "Template_v2(1).xlsx",
  size: 91827,
  type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  webkitRelativePath: ""
}

download:

const onDownload = d => {
    const link = document.createElement('a')
    link.download = d.name
    link.href = URL.createObjectURL(d) // method creates a string containing a URL representing the object
    link.click()
}

<Button onClick={onDownload(data)} variant="contained" color="primary">Download</Button>

Comments

-1

You can use the onClick event handler of the button to get the event callback and utilize the following code from the following link to download the file.

https://codesource.io/how-to-download-file-in-javascript/

8 Comments

unfortunately it opens the download link in the browser (eg: image) instead of saving it
It should be a full download link, then it shall download it.
It shall not. The link might be a link to page. See this example here to download an image. codesandbox.io/s/…
I think it has something to do with the url. For your url works. Can you check this url -- https://secure.gravatar.com/avatar/dde881ee7846ed21119d52f60dea7053
Yes, I did wget secure.gravatar.com/avatar/dde881ee7846ed21119d52f60dea7053.jpg and output is in next comment. Server do not allow the image to be download,
|
-1

The correct Material UI way is:

import {Button, Link} from '@mui/material'

<Button
    variant="contained"
    color="#ffa726"
    size="large"
    startIcon={<GetAppIcon />}
    component={Link}
    href="./abcf32x.pdf"
    download="How-to-download-file.pdf"
>
    Download Sample Method File
</Button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.