File System API - boot from local disk to local file system

Ive read a lot about the file system and HTML5 APIs, but I just couldn't find a working solution, so I ask you:

I want to have a file upload form, drag and drop or a regular input field, it doesn’t matter, however I want to select a file, and after downloading it, it needs to take the file or the whole folder and “upload” it to the file system located on the client computer. The download is in brackets because I really want to copy the file / folder to the local clients file system.

Is it possible? Because I want to make an application where the user can upload their files, such as music or large videos and movies, to their local file system and edit / watch, etc. In my application. I know that I have to upload those large files that I have to cut into pieces and load them in a stack, but I just want to start a little bit :)

Thanks in advance

+3
source share
2 answers

Currently, there is really little information on this, so I put together an example that brings together:

  • Use attribute webkitdirectoryon <input type="file">.
    • This allows the user to select a directory using the appropriate dialog box.
  • Using the file system API.
    • , .
  • API .
    • API, . <input type="file"> API .

Chrome, webkit, .

http://jsfiddle.net/zLna6/3/

, , , :

var fs,
    err = function(e) {
        throw e;
    };

// request the sandboxed filesystem
webkitRequestFileSystem(
    window.TEMPORARY,
    5 * 1024 * 1024,
    function(_fs) {
        fs = _fs;
    },
    err
);

// when a directory is selected
$(":file").on("change", function() {
    $("ul").empty();

    // the selected files
    var files = this.files;
    if(!files) return;

    // this function copies the file into the sandboxed filesystem
    function save(i) {
        var file = files[i];

        var text = file ? file.name : "Done!";

        // show the filename in the list
        $("<li>").text(text).appendTo("ul");

        if(!file) return;

        // create a sandboxed file
        fs.root.getFile(
            file.name,
            { create: true },
            function(fileEntry) {
                // create a writer that can put data in the file
                fileEntry.createWriter(function(writer) {
                    writer.onwriteend = function() {
                        // when done, continue to the next file
                        save(i + 1);
                    };
                    writer.onerror = err;

                    // this will read the contents of the current file
                    var fr = new FileReader;
                    fr.onloadend = function() {
                        // create a blob as that what the
                        // file writer wants
                        var builder = new WebKitBlobBuilder;
                        builder.append(fr.result);
                        writer.write(builder.getBlob());
                    };
                    fr.onerror = err;
                    fr.readAsArrayBuffer(file);
                }, err);
            }, 
            err
        );
    }

    save(0);
});

$("ul").on("click", "li:not(:last)", function() {
    // get the entry with this filename from the sandboxed filesystem
    fs.root.getFile($(this).text(), {}, function(fileEntry) {
        // get the file from the entry
        fileEntry.file(function(file) {
            // this will read the contents of the sandboxed file
            var fr = new FileReader;
            fr.onloadend = function() {
                // log part of it
                console.log(fr.result.slice(0, 100));
            };
            fr.readAsBinaryString(file);
        });
    }, err);
});
+4

, . file input, - , .

, , : 1) 2) API HTML5. №1, , , № 2 , . - , , , - .

, API Chrome ( ). , , IndexedDB. localStorage, Chrome 5 , .

+1

All Articles