大多数像样的web app在它们程序生命周期的某些环节需要对文件进行存取。HTML5 文件系统 API 给web 带来了一个合适的文件系统。不需要插件,过程也不繁琐。有了它,用户可以对数据持久化存入文件,也可以将文件夹持久化到经过沙盒处理的文件系统。(With it, users can persist data to files and folders to a filesystem sandboxed to your web app’s origin.)
当你已经通过这些API创建了一些文件之后,我敢说你可以去尝试一下3d版的命令行!(After you’ve created a few files, I dare you to try the 3d command!)
浏览器支持情况
目前只有 Chrome 支持 HTML5 文件系统 API。而且照现在的趋势来看,Mozilla将会坚定不移地将IndexedDB作为文件读取的唯一解决方案。在我看来,IndexedDB 如此复杂了使得用它来存储文件或者创建任何形式的文件夹视图变得几乎不可能。(IndexedDB is far too difficult to use for stashing files and creating any kind of a folder hierarchy. )而且还滥用API。在这样的过渡时期,我自己写了一个JS文件系统库idb.filesystem.js ,它能将 HTML5 文件系统 API 集成到那些只支持IndexedDB的浏览器。这就意味着任何支持IDB的浏览器(理论上来说)也能支持HTML5 文件系统 API。所以欢迎使用!
如果想更加深入的研究文件系统 API ,你可以去看看我的书《如何使用HTML5 文件系统 API》。除此之外,你还可以看看filer.js这个JS库。这是一个方便的包装库。它将API抽象成UNIX中一些命令(诸如cp,mv,mkdir等)。(It’s a handy wrapper library that abstracts the API into UNIX calls such as (cp, mv, mkdir))
4.Access native hardware(对本地硬件的操作)
看到这个标题也许你会很激动,“Whaaaat!? Web 不能对本地的硬件进行操作”。对于大部分情况我会给一个肯定的回答。令人悲伤的是,在当下的Web上访问诸如USB,蓝牙和UDP这样的硬件是一件不可能的事。尽管我们已经看到PhoneGap这样的框架在为Web访问硬件铺路,但现在的事实是现阶段的Web并不包含开发者一直在争取甚至争到口吐白沫的那些 API 。(We’ve seen frameworks such as PhoneGap pave the way here – but the fact remains, the drive-by web doesn’t have all of the APIs developers are foaming at the mouth for.)
web apps充分利用那些与底层硬件相关的高级的JS API 的能力不是一般的强(The ability for web apps to leverage high level JS APIs that sit on top of underlying hardware isn’t foreign web)
。最近几年还是有不少进展的( The last few years have brought us a bunch of this kind of stuff:)。
对于getUserMedia我最中意的是它对这个平台已存在的一些部分的重用,重用的部分就是HTML5
<video>。用法是将video.src 这个属性的值常用的视频文件换成了从摄像头产生的BLOB URL。此时视频便可源源不断地传过去了。这种集成就是一个新老API结合很好的例子。(What I really like about getUserMedia is that it’s reusing older parts of the platform, namely HTML5 <video>. Instead of setting the video.src to a movie file, we set it to a blob URL created from the camera stream. A live feed. This kind of integration is a great example of older APIs co-existing with newer ones.)
On the DJ machine: a) 使用
Web Audio API 的decodeAudioData() 方法对整个mp3文件进行解码。 b) 文件一旦解码完成便将整个
AudioBuffer划分成更小的块。我们不希望一次性发送整个 AudioBuffer 。 c) 使用一个简单的NodeJS 服务器将划分好的每块(打包成 ArrayBuffer)通过二进制 WebSocket进行发送。
On the listener’s machine: a) 当文件即将播放的时候在精确的时间点通过
Web Audio API 装载和编排收到的每个划分块。这种对音频的无缝“接合”在listener看来就像在播放整个文件。
通过WebRTC工作组的努力,也许文件共享的未来就是 DataChannel API 。不过不幸的是,现在它还只在Chrome 和 FireFox 中实现了(Unfortunately, it’s still being implemented in Chrome and FF)。这个API旨在让实时的端对端的数据传输变成可能。
浏览器支持情况
Chrome, FF, IE 10 和 Safari 支持 二进制 WebSockets。而 Web Audio API 现在仅有Chrome 和 Safari 支持。
总结
关于“本地和Web”的争论仍是一件让我头疼的事情。我是一个 Web 开发者。我不可能一点都不关心本地应用能做些什么同时我关心任何与Web能做的相关的事情!诚然,我们的Web 平台还有许多不尽如人意的地方,但是我们正在通过这些日益增加的API来解决这些差距。诸如
Web Components 这样的事物的出现将会改变我们构建Web App的方式。本着这种期待,我希望看到我们的 web 开发者不要再去参加那些无谓的争论而是将更多的精力转移到 web 能做的事情上来。还有许多人甚至没有意识到web 还能做些什么。( In this spirit, I’d like to see us web folk stop squaring off with the other guys and shift the conversation more around what the web platform can do. A lot of people don’t even realise what’s possible.)