在JavaScript中上传文件夹及其所有内容 [英] Upload folder and all its content in JavaScript

查看:81
本文介绍了在JavaScript中上传文件夹及其所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JavaScript(客户端)上传文件夹的内容? Chrome以外的浏览器尚未采用FileSystem API;我只获得一个带有文件夹名称的文件项。

How do I upload the content of a folder using JavaScript (client side)? The FileSystem API has not been adopted by browsers other than Chrome; I only get a File item with the name of the folder.

应该可以,因为Google Drive允许删除文件夹和所有内容(文件夹和文件)将自动上传。

It should be possible, because Google Drive allows to drop a folder and all the content (folders and files) will be uploaded automatically.

推荐答案

Chrome和Firefox似乎支持部分文件系统API,但不受官方支持。

It seems that Chrome and Firefox supports part of the filesystem API, but are not oficially supported.

这允许您删除文件夹并阅读所有内容,这是我在我的应用程序中使用的代码。

This allows you to drop a folder and read all the content, here's the code i use on my app.

    function add Files(e){
        e.stopPropagation();
        e.preventDefault();

        // if directory support is available
        if(e.dataTransfer && e.dataTransfer.items)
        {
            var items = e.dataTransfer.items;
            for (var i=0; i<items.length; i++) {
                var item = items[i].webkitGetAsEntry();

                if (item) {
                  addDirectory(item);
                }
            }
            return;
        }

        // Fallback
        var files = e.target.files || e.dataTransfer.files;
        if (!files.length)
        {
            alert('File type not accepted');
            return;
        }

        processFile(files);
    }

    function addDirectory(item) {
        var _this = this;
        if (item.isDirectory) {
            var directoryReader = item.createReader();
            directoryReader.readEntries(function(entries) {
            entries.forEach(function(entry) {
                    _this.addDirectory(entry);
                });
            });
        } else {
            item.file(function(file){
                processFile([file],0);
            });
        }
    },

这篇关于在JavaScript中上传文件夹及其所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆