Giter Site home page Giter Site logo

Implement download() method about filer.js HOT 4 OPEN

ebidel avatar ebidel commented on September 15, 2024
Implement download() method

from filer.js.

Comments (4)

micheg avatar micheg commented on September 15, 2024

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)});

from filer.js.

AdamSurfari avatar AdamSurfari commented on September 15, 2024

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.

from filer.js.

ebidel avatar ebidel commented on September 15, 2024

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 :(

from filer.js.

micheg avatar micheg commented on September 15, 2024

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.

from filer.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.