简体   繁体   English

您可以在本地创建输出要保存的.txt文件的HTML表单吗?

[英]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? 您是否可以创建一个HTML表单,该表单可以放在USB闪存驱动器上,并可以在浏览器中打开,从而允许某人输入信息,然后允许他们将输入的.txt文件保存回同一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. 生成带有表单的普通HTML页面,以输入所有必要的信息。

  2. Then use JavaScript to build a string containing all data you want to store inside the textfile. 然后使用JavaScript构建一个字符串,其中包含要存储在文本文件中的所有数据。

  3. Create a Blob() object out of it ( MDN docu ) - the type application\\octet-stream is important to force a download later on: 从其中创建一个Blob()对象( MDN docu )-类型application\\octet-stream对于强制稍后进行下载很重要:

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

  4. Convert that blob to a DataURL using window.URL.createObjectURL ( MDN docu ): 使用window.URL.createObjectURLMDN window.URL.createObjectURL )将那个Blob转换为DataURL:

    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更新浏览器标签的位置,并将其设置为数据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. 由于这种形式的表单处理必须在服务器端进行,因此需要Web服务器。

Now, it is entirely possible to run Apache or something similar from that flash drive, and have PHP or something do your file writing. 现在,完全有可能运行Apache或该闪存驱动器中的类似文件,并使用PHP或其他文件来编写文件。 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. 请记住,并非每个人都启用了自动运行功能,人们使用的操作系统不同,并且防火墙经常对打开端口的新应用程序保持挑剔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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