简体   繁体   中英

Can you locally create a HTML form that outputs a .txt file to be saved?

Can you create a html form that can be housed on a USB flash drive and opened up in a browser that allows someone to enter info and then allows them to save what they entered as a .txt file back to the same USB? Any ideas or resources you can point me to?

Not a full solution, but maybe it gets you on the right track:

  1. Generate a usual HTML page with a form to enter all information necessary.

  2. Then use JavaScript to build a string containing all data you want to store inside the textfile.

  3. Create a Blob() object out of it ( MDN docu ) - the type application\\octet-stream is important to force a download later on:

    var myBlob = new Blob( content, { "type" : "application\\/octet-stream" });

  4. Convert that blob to a DataURL using window.URL.createObjectURL ( MDN docu ):

    var dataUrl = window.URL.createObjectURL( myBlob );

  5. Update the location of your browser tab using window.location and set it to your data url:

    window.location = dataUrl;

The user will then have the usual "Save file as ..." dialog for your generated textfile. Note, however, that this way you are not able to set the name of the textfile!

Not directly. Since this type of form processing has to occur server-side, you need a web server.

Now, it is entirely possible to run Apache or something similar from that flash drive, and have PHP or something do your file writing. This isn't as straightforward as you are looking for, but is possible. Keep in mind that not everyone has autorun enabled, that folks use different OSes, and that firewalls are often picky about new applications opening up ports.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM