1

I have a table in some cells I have URLs, and I need after click save files from URLs. I found example with Download Image - but it doesn't work, when I clicked I see image on full screen; I need after save file in choosen place.

https://codesandbox.io/s/lucid-wave-pc69e?file=/src/App.js

Table column:

  {
    title: '3D model file',
    dataIndex: 'file',
    key: 'file',
    render: (file: string) => {
      if (file) {
        return <a href={file} onClick={(e) => e.stopPropagation()} download>Download</a>;
      }
    }
  },
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Dell Opp
  • 41
  • 6

1 Answers1

0

You've got two options:

  • make the server tell the browser the file should be downloaded instead of viewed in the browser

    To do this, you'd make the server include the HTTP header Content-Disposition: attachment

  • or, transfer the file data using JavaScript, and then save it to disk using JavaScript

    Once your JavaScript has acquired the data, you'd create an <a download="DESIRED_FILENAME">, construct an ObjectURL using the data and stuff it into the href, then programmatically click the anchor


The first option is by far the easiest and yields excellent results across all browsers and platforms. The only major drawback is that it requires making code changes on the server. The change is typically small, although sometimes extant code is a hot mess and so even easy things are hard.

The second option works great for desktop browsers but yields absolutely terrible results for almost all mobile browsers. And it might require code changes to the server -- I don't know offhand whether the data can be transferred in its normal binary or whether it would need to be encoded for transport using something like base64. (I've only done this with base64-encoded data, because that's what my server supported.) I also suspect implementation could be a significant challenge for a developer who is unfamiliar with JavaScript and doesn't know how to do their own research.

It might be possible to avoid the UX drawbacks of the second option by using a WebWorker to supply the Content-Disposition header, but I've not tried it.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Tom
  • 8,509
  • 7
  • 49
  • 78