how to download file in react js

前端 未结 12 1627
梦谈多话
梦谈多话 2020-12-01 05:49

I receive file url as response from api. when user clicks on download button, the file should be downloaded without opening file preview in a new tab. How to achieve this in

相关标签:
12条回答
  • 2020-12-01 06:25

    This is how I did it in React:

    import MyPDF from '../path/to/file.pdf';
    <a href={myPDF} download="My_File.pdf"> Download Here </a>
    

    It's important to override the default file name with download="name_of_file_you_want.pdf" or else the file will get a hash number attached to it when you download.

    0 讨论(0)
  • 2020-12-01 06:27

    You can use FileSaver.js to achieve this goal:

    const saveFile = () => {
    fileSaver.saveAs(
      process.env.REACT_APP_CLIENT_URL + "/resources/cv.pdf",
      "MyCV.pdf"
    );
    

    };

    <button className="cv" onClick={saveFile}>
        Download File
    </button>
    
    0 讨论(0)
  • 2020-12-01 06:30

    I have the exact same problem, and here is the solution I make use of now: (Note, this seems ideal to me because it keeps the files closely tied to the SinglePageApplication React app, that loads from Amazon S3. So, it's like storing on S3, and in an application, that knows where it is in S3, relatively speaking.

    Steps

    3 steps:

    1. Make use of file saver in project: npmjs/package/file-saver (npm install file-saver or something)
    2. Place the file in your project You say it's in the components folder. Well, chances are if you've got web-pack it's going to try and minify it.(someone please pinpoint what webpack would do with an asset file in components folder), and so I don't think it's what you'd want. So, I suggest to place the asset into the public folder, under a resource or an asset name. Webpack doesn't touch the public folder and index.html and your resources get copied over in production build as is, where you may refer them as shown in next step.
    3. Refer to the file in your project Sample code:
      import FileSaver from 'file-saver';
      FileSaver.saveAs(
          process.env.PUBLIC_URL + "/resource/file.anyType",
          "fileNameYouWishCustomerToDownLoadAs.anyType");
      

    Source

    • Create-React-App.dev | Using the public folder
    • Github | File saver documentation

    Appendix

    • I also try Link component of react-router react-router-docs/Link. The zip file would download, and somehow would unzip properly. Generally, links have blue color, to inherit parent color scheme, simply add a prop like: style={color: inherit} or simply assign a class of your CSS library like button button-primary or something if you're Bootstrappin'
    • Other sage questions it's closely related to:
      • download file in react
      • how to download file in react js
      • button to download a file in reactJS
      • download sample file in public folder (react)
    0 讨论(0)
  • 2020-12-01 06:35

    You can define a component and use it wherever.

    import React from 'react';
    import PropTypes from 'prop-types';
    
    
    export const DownloadLink = ({ to, children, ...rest }) => {
    
      return (
        <a
          {...rest}
          href={to}
          download
        >
          {children}
        </a>
      );
    };
    
    
    DownloadLink.propTypes = {
      to: PropTypes.string,
      children: PropTypes.any,
    };
    
    export default DownloadLink;
    
    0 讨论(0)
  • 2020-12-01 06:36

    Triggering browser download from front-end is not reliable.

    What you should do is, create an endpoint that when called, will provide the correct response headers, thus triggering the browser download.

    Front-end code can only do so much. The 'download' attribute for example, might just open the file in a new tab depending on the browser.

    The response headers you need to look at are probably Content-Type and Content-Disposition. You should check this answer for more detailed explanation.

    0 讨论(0)
  • 2020-12-01 06:38

    We can user react-download-link component to download content as File.

    <DownloadLink
    label="Download"
    filename="fileName.txt"
    exportFile={() => "Client side cache data here…"}/>
    

    https://frugalisminds.com/how-to-download-file-in-react-js-react-download-link/

    0 讨论(0)
提交回复
热议问题