简体   繁体   English

文件上传并了解目录结构

[英]File Upload and knowing the directory structure

We are using jquery fileupload to drag and drop files (and folders) from a local computer to a browser. 我们使用jquery fileupload将文件(和文件夹)从本地计算机拖放到浏览器。 This works great but we can't capture the directory structure of the files within the folder. 这很好但我们无法捕获文件夹中文件的目录结构。 I understand why (from a security perspective and javascript) this doesn't work, but does anyone have any thoughts on best ways to achieve the same thing. 我理解为什么(从安全角度和javascript)这不起作用,但是有没有人对实现同样事情的最佳方法有任何想法。

Again, I want my customer (internal app) to drag and drop a folder into my application. 同样,我希望我的客户(内部应用程序)将文件夹拖放到我的应用程序中。 My application can see the list of filenames and they get uploaded, but I would like to maintain the directory structure of those files for use elsewhere. 我的应用程序可以看到文件名列表并上传,但我想维护这些文件的目录结构以供其他地方使用。 ie, it's important for me to know that it came from directory x/1/a rather than y/2/b. 也就是说,重要的是我知道它来自目录x / 1 / a而不是y / 2 / b。

Thanks in advance! 提前致谢!

See jquery file upload's support for this related to @Dead133s mention of webkitdirectory https://github.com/blueimp/jQuery-File-Upload/wiki/Browser-support 请参阅jquery文件上传对此相关的支持@ Dead133s提及webkitdirectory https://github.com/blueimp/jQuery-File-Upload/wiki/Browser-support

"It is possible to select a complete folder structure, though this is currently only supported by Google Chrome.To enable this feature, the following vendor-specific directory attributes have to be added to the file input field:" “可以选择完整的文件夹结构,但目前只有谷歌Chrome支持。要启用此功能,必须将以下特定于供应商的目录属性添加到文件输入字段:”

<input type="file" name="files[]" multiple directory webkitdirectory mozdirectory>

Another low-tech solution would be to have users zip up the files and upload those, preserving any folder. 另一种低技术解决方案是让用户压缩文件并上传文件,保留任何文件夹。

File API: Directories and System is currently a W3C Working Draft and already works in webkit, works in latest Chrome and Safari. 文件API:目录和系统目前是W3C工作草案,已经可以在webkit中使用,可以在最新的Chrome和Safari中使用。

There is a nice file upload example, you can dropdown a directory and see it's structure: http://sapphion.com/2012/06/keep-directory-structure-when-uploading/ 有一个很好的文件上传示例,您可以下拉目录并查看其结构: http//sapphion.com/2012/06/keep-directory-structure-when-uploading/

Awesome html5rocks tutorial: http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-dir 令人敬畏的html5rocks教程: http ://www.html5rocks.com/en/tutorials/file/filesystem/#toc-dir

You can achieve this using either a custom implementation of the filesystem API similar to this or even just using DropzoneJS and then using an algorithm similar to the one below to build a hash map of the directories and files that belong in each directory. 您可以使用与此类似的文件系统API的自定义实现,甚至只使用DropzoneJS ,然后使用类似于下面的算法来构建属于每个目录的目录和文件的哈希映射。 I've included some sample code below that should push you in the right direction. 我在下面提供了一些示例代码,可以帮助您朝着正确的方向前进。

        uploadFilesDepthFirst(folderId, folderInfo) {
            Object.keys(folderInfo.children).forEach(childFolderName => uploadFilesDepthFirst(folder.id, folderInfo.children[childFolderName]));
            folderInfo.files.map(file => uploadFile(folderId, file.file));
        }

        let fileList = files.map((file) => { return {path: file.fullPath, filename: file.name , file: file} });

        const hierarchy = {}; // {folder_name} = { name: <name of folder>, children: {...just like hierarchy...}, files: [] }
        // build tree
        fileList.map(file => {
            const paths = file.path.split('/').slice(0, -1);
            let parentFolder = null;
            // builds the hierarchy of folders.
            paths.map(path => {
                if (!parentFolder) {
                    if (!hierarchy[path]) {
                        hierarchy[path] = {
                            name: path,
                            children: {},
                            files: [],
                        };
                    }
                    parentFolder = hierarchy[path]
                } else {
                    if (!parentFolder.children[path]) {
                        parentFolder.children[path] = {
                            name: path,
                            children: {},
                            files: [],
                        };
                    }
                    parentFolder = parentFolder.children[path];
                }
            });
            parentFolder.files.push(file);
        });

        Object.keys(hierarchy).map(folderName => uploadFilesDepthFirst(parentId, hierarchy[folderName]));

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

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