ebidel/filer.js

Implement download() method

Opened this issue · 4 comments

Uses a[download]

I use a simple method for download files,

// need chrome --disable-web-security or whitelist in manifest!

    /**
    * download up and return a local url string for a given URL entry.
    *
    * @param {string} url a file url
    * @param {string|FileEntry} entryOrPath A path, filesystem URL, or FileEntry
    *     of the file to lookup.
    * @param {Function} successCallback Success callback passed the File object.
    * @param {Function=} opt_errorHandler Optional error callback.
    */

    Filer.prototype.download = function(remoteURL, entryOrPath, successCallback, opt_errorHandler)
    {
        var xhr = new XMLHttpRequest();
        xhr.responseType='blob';
        xhr.open("GET", remoteURL, true);
        tmp_filer = this;
        xhr.onload = function(e)
        {
            if(this.status == 200)
            {
                var buff = xhr.response;
                tmp_filer.write(entryOrPath, {data: buff, type: buff.type}, function(fileEntry, fileWriter)
                {
                    if(successCallback) successCallback(fileEntry.toURL());
                }, opt_errorHandler);
            }
            else
            {
                if(opt_errorHandler) opt_errorHandler();
            }
        }
        xhr.onerror = function (e) {if(opt_errorHandler) opt_errorHandler();}
        xhr.send();
    }

    test_filer = new Filer();
    test_filer.init({persistent: true, size: 5*1024*1024}, function(fs)
    {
        test_filer.download('http://learningfromdogs.files.wordpress.com/2010/09/cats-in-sink.jpg', './cats-in-sink.jpg', function (path) {console.log(path);});
    }, function () {console.log(error)});

Just a simple question. If I only need to download files hosted in my domain I will not get Origin is not allowed by Access-Control-Allow-Origin errors?

I am writing an application that downloads texture packs in ZIP format, and caches them in the FileSystem. All packs will be provided by the same server, so I do not think there will be issues, but I would like to make confirm that.

Correct. If you're XHRing files from the same origin, you won't get errors or need to use flags.

@micheg it's a really bad idea to turn on --disable-web-security in your primary browser :(

I used it not in the system browser, I used this method inside an app in a chromium portable browser, created for the purpose.
Now I'm rewriting that app for nw.js, I'm still undecided whether to use the HTML5 storage or node modules for filesystem.