简体   繁体   English

HTML5 / JavaScript:打开文本文件,加载到文本区域/将文本区域内容保存到文本文件

[英]HTML5 / JavaScript: open text file, load into textarea / save textarea content to text file

I want to do two things in my browser:我想在我的浏览器中做两件事:

  • Load a text file into a textarea (has to be choosen via dialog box)将文本文件加载到文本区域(必须通过对话框选择)
  • Save the content of a textarea into a text file (has to be choosen via dialog box again)将文本区域的内容保存到文本文件中(必须再次通过对话框选择)
  • Load a video file and grab the file path to use it with a video player ( 1 )加载视频文件并获取文件路径以将其与视频播放器一起使用 ( 1 )

I've been looking around for a while on the inte.net.我已经在 inte.net 上四处寻找了一段时间。 There are some solutions for IE only via ActiveXObjects, which I can't use (IE, seriously?).有一些仅通过 ActiveXObjects 的 IE 解决方案,我不能使用(IE,认真的吗?)。 HTML5 file API has limited usability because I can't access the selected file's path. HTML5 文件 API 的可用性有限,因为我无法访问所选文件的路径。

I also found save dialogs for textareas, but they ignored line breaks for some strange reason and I don't know how to fix that, if possible at all.我还找到了文本区域的保存对话框,但由于某些奇怪的原因,它们忽略了换行符,我不知道如何解决这个问题,如果可能的话。

So here are my requirements and options:所以这是我的要求和选择:

  • Support for FF and Chrome支持 FF 和 Chrome
  • JavaScript, HTML5 (and PHP, if it has to be) JavaScript、HTML5(和 PHP,如果必须的话)
  • possibly Silverlight, but I'm not very familiar with it and may only copy and paste:-/可能是 Silverlight,但我不是很熟悉它,只能复制和粘贴:-/
  • it has to work on Mac as well它也必须在 Mac 上工作

There is a dirty hack that gets the job done without resorting to Flash or Silverlight, or using a server, and it works in most browsers:有一个肮脏的 hack 可以在不求助于 Flash 或 Silverlight 或使用服务器的情况下完成工作,并且它适用于大多数浏览器:

var uriContent = "data:application/octet-stream," + encodeURIComponent(fileContentsAsString);
window.open(uriContent, 'Save Your File');

JS runs in a sandbox. JS 在沙箱中运行。 That means: no access to files on the filesystem.这意味着:无法访问文件系统上的文件。 HTML5 file API is the first „native” (as in not flash nor activex) attempt to grant limited access to the users file system. HTML5 文件 API 是第一个“本地”(不是 flash 也不是 activex)尝试授予对用户文件系统的有限访问权限。

The File API is HTML that would allow you to access data, after which you can manipulate binary blobs in JavaScript, but as written this is not possible in pure JS and HTML based on your requirements.文件 API 是 HTML,它允许您访问数据,之后您可以在 JavaScript 中操作二进制 blob,但是根据您的要求,这在纯 JS 和 HTML 中是不可能的。

The big blocker is "saving to a text file."最大的障碍是“保存到文本文件”。 The only way I've been able to do this is by opening up an iFrame that calls a server side language (such as PHP) to set the content type in the header to a type that prompts a download.我能够做到这一点的唯一方法是打开一个调用服务器端语言(例如 PHP)的 iFrame,将 header 中的内容类型设置为提示下载的类型。

Flash and Silverlight are "client" technologies that run outside of the sandbox, which sounds like your only option at this point. Flash 和 Silverlight 是在沙箱之外运行的“客户端”技术,这听起来像是您目前唯一的选择。

My ideas:我的想法:

Load a text file: Use a normal HTML upload form (if you want to script, maybe submit it via AJAX)加载文本文件:使用普通的 HTML 上传表单(如果你想编写脚本,可以通过 AJAX 提交)

Save a text file: Use a textarea and upon submitting, create the file server-side and then offer it to download.保存文本文件:使用文本区域并在提交后在服务器端创建文件,然后提供下载。 (As mentioned before, client-side scripts do not have access to the computer's file system) (如前所述,客户端脚本无法访问计算机的文件系统)

Load a video file: Is the video on the server already?加载视频文件:服务器上是否已经有视频? Otherwise will need an upload just like the text file.否则将需要像文本文件一样上传。 Then use a flash plugin to play the file from the server (the URI should be known to you then)然后使用 flash 插件从服务器播放文件(到时候你应该知道 URI)

All of these are relatively simple to achieve using PHP. Line breaks from a textarea stay as \n in PHP. Tutorials: Form handling in PHP , File upload in PHP所有这些都相对简单地使用 PHP 来实现。textarea 的换行符在 PHP 中保持为 \n。教程:PHP 中的表单处理,PHP中的文件上传

Edit: Since PHP runs server-side you should not run into a lot of problems because of browser diversity.编辑:由于 PHP 在服务器端运行,您应该不会因为浏览器的多样性而遇到很多问题。 Alternatively, you can do all of these in Flash or Silverlight as well, although from my point of view that takes more learning and is less comfortable for the user.或者,您也可以在 Flash 或 Silverlight 中完成所有这些操作,尽管从我的角度来看,这需要更多的学习并且对用户来说不太舒服。

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

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