-2

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!

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
My Car
  • 4,198
  • 5
  • 17
  • 50
  • Hi @CryptoFool, "a HTML text field in Python" means I read HTML code in Python, and HTML code is the code of the text field – My Car Oct 26 '22 at 02:57
  • Ok, so your Python code reads in HTML. What does it do with it? How does Python code reading HTML from a file lead to a field into which the user can enter a string? – CryptoFool Oct 26 '22 at 03:02
  • I'm assuming your are serving a web app from python and want the user to download text? Does the backend server need to know about that text or can you implement a solution just in javascript like [this](https://stackoverflow.com/questions/3665115/how-to-create-a-file-in-memory-for-user-to-download-but-not-through-server)? – Modularizer Oct 26 '22 at 03:12
  • Hi @Modularizer, the backend server (Python) knows the text because its task is to _**get**_ the text from the HTML and download it – My Car Oct 26 '22 at 03:20
  • @MyCar it is possible for javascript in the browser to download a file into the `Downloads` folder of the user when they click a button without any need to send a web request to the backend server of for the backend to even know the file was downloaded. Would this work for you? Or are you trying to download the file to a specific location on the server machine that is running the webpage? – Modularizer Oct 26 '22 at 03:25
  • @MyCar to my knowledge there is no way of adding a file to a users **Desktop** unless that user is accessing the webpage on the same machine that is serving the webpage (unlikely). You can, however, let the user download a file to their own **Downloads** folder – Modularizer Oct 26 '22 at 03:27
  • @Modularizer probably does as you say let users download files into their own downloads folder. Just, where to write JavaScript code? – My Car Oct 26 '22 at 09:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/249090/discussion-between-modularizer-and-my-car). – Modularizer Oct 27 '22 at 00:53

1 Answers1

1

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.

Modularizer
  • 611
  • 4
  • 8