Javascript FileAPI: Iterating Directories?

江枫思渺然 提交于 2019-12-01 09:39:07

问题


Firefox 3.6 let's you drop a directory onto some element (using the Drag And Drop API) and process those files with the FileAPI. How does that work with directories?

The Mac OS X Finder displays some directories as Files (.app, .tmbundle, .abbu, …). In most cases this is wonderful. In the case of interacting with the browser via File- and DND-APIs it's not. The end-user is incapable of differentiating between a real file and a sort-of-file like my-addressbook-backup.abbu.

The Javascript UI must therefore be able to identify directory structures. If it's not capable of traversing the structure, it must inform the user accordingly.

Any ideas on how either can be accomplished?

Edit:

I can see the (potential) problems arising when allowing JS to traverse directories. I'm not calling for a directory traversal per se (although I wouldn't bind being able to). I'm talking about Foo.app being treated as a file (as so seen by the user through Finder.app) but actually being a directory (as so seen when looking at it in Terminal.app).

The File API currently doesn't provide any traversing mechanisms. So, deeper nested files within an addressbook-dump.abbu wouldn't be accessible. Are there any thoughts on making this possible?

Otherwise I'd have to tell my OSX users to make an archive (e.g. ZIP) of their address book file and "upload" that zip (which I can actually read in JS). Though this is a workaround, not a solution.


回答1:


Theoretically, what you describe is doable with the FileReader API. For example, this demo reads a .zip file (hit load photos.zip), unpacks its contents, and displays the list of pictures it contains to the user.

For general folder drag and drop, there are two bugs holding Chrome/WebKit back: 1, 2. When those get fixe, you'll be able to drag a folder from the OS onto an <input type="file" webkitdirectory> and traverse all its hierarchy. To see webkitdirectory in action, check out this demo in Chrome.



来源:https://stackoverflow.com/questions/4238269/javascript-fileapi-iterating-directories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!