Home > Software design >  How can I download a .txt file from a string and save it to the user's desktop in Python?
How can I download a .txt file from a string and save it to the user's desktop in Python?

Time:10-27

I have a HTML text field in Python. (I read the HTML code in Python, the HTML code is a text field) When the user enters a string in the text field and clicks the submit button, I want the application to automatically download a .txt file from the string and save it to the user's desktop in Python.

My code:

<form action="/a" method="POST">
    <input
      type="text"
      value="String"
      name="string"
    />

output = request.form.to_dict()
string = output["string"]

I've tried googling and reading some Stack Overflow questions, but still can't find some that are relevant to me. This is irrelevant to me because I want to save it to the computer user's desktop, not a specific user's desktop.

So, how to do it? I would appreciate any help. Thank you in advance!

CodePudding user response:

For this particular application, it would be more suitable to do the download in the browser using javascript only. Because you are not trying to modify the text using data stored on your server, your server backend application (Python), does not need to know that the user is downloading the file. This is good because it will reduce load on your server and the user will be able to perform the download even if they temporarily lose internet connection.

Unfortunately for you, but fortunately for the security of the user, you don't get to decide the location at which to download the file onto the user's computer, so you can't make it save to their desktop. Typically it will go into their downloads folder. You can, however, set the filename to whatever you want.

The following solution is mostly taken from @MatějPokorný's answer here and should work for you.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script>
        function download(filename, text) {
          /*
          code from StackOverflow user Matěj Pokorný at
          https://stackoverflow.com/questions/3665115/how-to-create-a-file-in-memory-for-user-to-download-but-not-through-server#annswer-18197341
          */
          var element = document.createElement('a');
          element.setAttribute('href', 'data:text/plain;charset=utf-8,'   encodeURIComponent(text));
          element.setAttribute('download', filename);

          element.style.display = 'none';
          document.body.appendChild(element);

          element.click();

          document.body.removeChild(element);
        }
    </script>
    <form onsubmit="download('webpage_entry.txt', this['string'].value)" method="POST">
        <input
          type="text"
          value="String"
          name="string"
        />
    </form>

</body>
</html>

If you for whatever reason did want to involve the backend in the download, you could set up a handler on your server which receives POST requests to route /a and responds with file data.

  • Related