-1

I am not an HTML/JavaScript developer. I am having to modify some legacy code written by someone who has left. We have a Python app which acts as a local server with an HTML/JavaScript front end that can be viewed in a browser.

The Python creates a temporary cache file. I would like to give the user the option to save a copy of this temp file to a location of their choice or at least download it to the downloads directory (Windows & Linux)

I've tried adapting some of the ideas from here: https://www.delftstack.com/howto/javascript/javascript-download/

E.g.

    const saveAnalysisBtn = document.getElementById("saveAnalysisBtn");
    saveAnalysisBtn.addEventListener('click', saveAnalysis);

function saveAnalysis(evt) {

    function download(filename) {
          var element = document.createElement('a');
          // hardcode temp file name just for POC
          element.setAttribute('href','file://C:\\tmp\\my_temp_cache.db');
          element.setAttribute('download', filename);
          document.body.appendChild(element);
          element.click();
          //document.body.removeChild(element);
    }

    var filename = "output.txt";
    console.log(`Call Download`);
    download(filename);
}

In Firefox this gives a security error:

Security Error: Content at http://127.0.0.1:5000/replay/fapi_15_6_udi.bin may not load or link to file:///C:/tmp/my_temp_cache.db

Which isn't terribly surprising. (Edge & Chrome give similar errors)

Is there a way to do this? Can be in HTML or JavaScript or Python (though I would like user to see evidence of download taking place in the browser).

quite68
  • 39
  • 4

1 Answers1

0

Maybe I'm not understanding, but it looks like we're talking about just copying a file from one local location to a user specified location. The file you want to copy is on the machine the user is using? Couldn't you just provide the location in the web page and then just go there in a file explorer, finder, or command line tool to copy it however you want? It would solve the security issue.

But if you're required to create a link, you could create a download process that zips the file up to make a file like "my_temp_cache_db.zip" (or whatever compression tool/extension works best for you), and then provide the link for that. Zip files work through browsers better than some other types of files, and the user just has to unzip it wherever it ended up.

If that's not ideal, you could create a download process that makes a copy of the file and just changes the extension to something like "txt". The user downloads that file and then has to rename it to have the right extension.

Kevin
  • 175
  • 8
  • "Maybe I'm not understanding, but it looks like we're talking about just copying a file from one local location to a user specified location. The file you want to copy is on the machine the user is using? Couldn't you just provide the location in the web page and then just go there in a file explorer, finder, or command line tool to copy it however you want? It would solve the security issue." -- You are correct. However this is a bit complicated for the user, I'd like to make it really simple for them. – quite68 Aug 22 '22 at 14:16
  • Zips give the same security errors as other files. – quite68 Aug 22 '22 at 14:18