Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Downloading file onClick is downloading on refresh

In my documents table I’m mapping through some metadata to get a filename and docid that is getting passed to my DocumentDownloadButton:

const DocumentsTableBody = ({ documentMetadata, tableProps }) => {
  const { Row, Data } = Table

  return (
    documentMetadata.map(doc => {
      return (
        <Row {...tableProps} key={doc.id}>
          <Data>{formatDate(doc.creationDate)}</Data>
          <Data>
            <DocumentNameWrapper>
              {doc.name}
            </DocumentNameWrapper>
          </Data>
          <DocumentDownloadButton fileName={doc.name} docId={doc.id} />
        </Row>)
    })
  )
}

From my DocumentDownloadButton I’ve created a function to download the file taking those two props to download onclick.
The problem is it’s downloading the file on refresh even before I’ve opened the panel which is where the click event happens

const DocumentDownloadButton = ({ docId, fileName }) => {
  const { apiFor } = useAxios()

  const downloadDocument = (id, file) => {
    apiFor('someApi')
      .get(`/documents/${id}`, { responseType: 'blob' }, {
        headers: {
          Accept: 'applicaton/octet-stream'
        } })
      .then((response) => {
        // add loading state
        const contentType = response.headers['content-type'] || 'application/octet-stream'
        const blob = new Blob([response.data], { type: contentType })
        return FileSaver.saveAs(blob, file)
      })
      .catch((error) => {
        console.error(error)
      })
  }

  return (
    <>
      <DownloadIconContainer onClick={downloadDocument(docId, fileName)}>
        <DownloadIconSmall />
      </DownloadIconContainer>
    </>
  )
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That’s because you’re invoking the download function immediately rather than passing a reference of the function to the onClick. This should give you the intended behavior:

const DocumentDownloadButton = ({ docId, fileName }) => {
  const { apiFor } = useAxios()

  const downloadDocument = (id, file) => {
    apiFor('someApi')
      .get(`/documents/${id}`, { responseType: 'blob' }, {
        headers: {
          Accept: 'applicaton/octet-stream'
        } })
      .then((response) => {
        // add loading state
        const contentType = response.headers['content-type'] || 'application/octet-stream'
        const blob = new Blob([response.data], { type: contentType })
        return FileSaver.saveAs(blob, file)
      })
      .catch((error) => {
        console.error(error)
      })
  }

  return (
    <>
      <DownloadIconContainer onClick={() => downloadDocument(docId, fileName)}>
        <DownloadIconSmall />
      </DownloadIconContainer>
    </>
  )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading