chrisben / imgcache.js

JS library based on the File API to cache images for offline recovery (target: cordova/phonegap & chrome)
Other
826 stars 216 forks source link

Download error code 3 #37

Open Steefkroon opened 10 years ago

Steefkroon commented 10 years ago

I am having a problem downloading the images. Whenever I try to i get the following error: Download error code: 3

What am I doing wrong?

chrisben commented 10 years ago

Are you using Phonegap/Cordova? If so, please state the version you're using and the OS you're targeting. Is it a problem you get every time or intermittently? Could be linked to this cordova issue that plagues several versions up to the latest one.

Steefkroon commented 10 years ago

I am using cordova version 3.2.0 and ios 7, but i have the same problem on android 4.2. The images are download through a https request but that should not be a problem iirc. The issue occures everytime. I have tried to retry it like in the issue you linked me to but to no avail.

chrisben commented 10 years ago

Please make sure you don't have a CORS issue -- read the Readme file carefully about this topic and check out this stackoverflow answer.

pozuelog commented 10 years ago

Same here,

I'm testing on Android 2.3.3 & 4.1.1 in Cordova 4.3.

CORS is ok. I'm downloading a large quantity of files at the same time (about 200-300 images). It happens with about 10% of files. This is my code:


...
for(var j = 0; j<imagenesMonumentLength; j++){
                    setTimeout(
                        guardaImagen(imagenesMonumento[j].imagen), 1000*j);
}
...

function guardaImagen(image){
    porCachear++;
    ImgCache.cacheFile(image, function(){
        porCachear--;
            if(rutasRestantesNetwork <= 0 && porCachear <=0){
                    createMenuAndChangePage();
                }
    }, function(){
        porCachear--;
            if(rutasRestantesNetwork <= 0 && porCachear <=0){
                    createMenuAndChangePage();
                }
    });
}
chrisben commented 10 years ago

Look at this blog post, there's a workaround for this kind of problem (though it's an upload issue there...). One of the solutions is to set the "Connection" http header to "close".

The latest code in master now includes a new option for setting custom http headers, so you'll be able to test this solution this way:

ImgCache.options.headers = { 'Connection': 'close' };

If this does not work, try the chunkedmode = false by changing line 449 of imgcache.js to:

if (this.fileTransfer) return this.fileTransfer.download(uri, localPath, success_callback, error_callback, false, { 'headers': headers, 'chunkedMode': 'false' });

If nothing is solved after those changes, please look at your server logs to check for errors/warnings there.

pozuelog commented 10 years ago

By doing

ImgCache.options.headers = { 'Connection': 'close' };

I reduced the percentage of failed downloads. By investigating at server logs y discovered that it doesn't support so much petitions at the same of that (some files are mp3 songs).

I finally solved it by implementing a resources stack and doing only two downloads a time.

chrisben commented 10 years ago

@Steefkroon , could you try what @pozuelog suggested to see if that solves your problem as well?

chrisben commented 10 years ago

@Steefkroon can I close this issue?

confile commented 9 years ago

+1 I have a similar issue here: https://github.com/chrisben/imgcache.js/issues/85

Any idea how to solve it?

chrisben commented 9 years ago

@confile , #85 is a different issue, even if you get the same error code from cordova's file plugin.

tomcooksey commented 9 years ago

Hi, I am getting this issue as well. I am writing an iPad app wrapped in Cordova and I am using imgcache for the management of offline resources. In Chrome everything works fine, so when the when app is offline the images are cached correctly. However, once installed onto an iOS device (or simulator), all of the images, according to the logging, have been cached on the device, but when blocking the internet connection of the device some of the images cannot be loaded. I am using the library in such a way that I immediately try to cache an image and only if that process fails do I then return the already cached file...is this a work patter that is supported? If not do you have a better way to do it? I want to make sure that each time I get an image the cache is overwritten as the image may remain the same name but change content.

Error from one of the images that won't load:

[Log] ERROR: Download error source: http://res.cloudinary.com/xxxxxx/image/fetch/http://s3-eu-west-1.amazonaws.com/xxxxxJPG (index.html, line 96) [Log] ERROR: Download error target: file:///Users/xxxxx/Library/Developer/CoreSimulator/Devices/33F7AD56-551E-4AB9-BD52-45B690A0B502/data/Containers/Data/Application/3A1F5B6B-D14E-46A2-8B8B-4BE815F75531/Documents/imgcache//f1f2c6b32627c76afc36dde2e0e5a186266d14ac.jpg (index.html, line 96) ERROR: Download error code: 3

Code where I manage the saving:

getCachedImage: function(url, callback) {

        Framework.util.Log.logEvent('Attempting to get a cached image ' + url);

        //Get original cached file
        ImgCache.getCachedFileURL(url, function(original, cached) {
            console.log('Getting currently cached image');

            Framework.util.Log.logEvent('Image was already cached ' + url);

            var currentlyCached = cached;

            //Now try and cache what we have
            ImgCache.cacheFile(url, function(){

                //If we successfully managed to cache it, we grab the new one out of the cache
                ImgCache.getCachedFileURL(url, function(original, newlyCached) {

                    Framework.util.Log.logEvent('Got newly cached image ' + url);

                    console.log('Using newly cached image when original was present');
                    callback(newlyCached);
                });

            }, function() {
                console.log('Using previously cached image');
                Framework.util.Log.logEvent('Could not cache image so using previously cached ' + url);
                //If the caching failed, it means we're offline, so return original cached value
                callback(currentlyCached);
            });

        }, function(original) {

            Framework.util.Log.logEvent('No image already cached ' + url);

            //If we had no original, then we try and cache it
            ImgCache.cacheFile(url, function(){

                Framework.util.Log.logEvent('Newly cached ' + url);

                //If we successfully managed to cache it, we grab the new one out of the cache
                ImgCache.getCachedFileURL(url, function(original, newlyCached) {

                    Framework.util.Log.logEvent('Got newly cached image from cache ' + url);

                    console.log('Using newly cached image');
                    callback(newlyCached);
                });

            }, function() {

                Framework.util.Log.logEvent('Image not already cached and couldn\'t cache new image so just returning URL ' + url);

                console.log('We didnt already have the cached image, and we failed trying to cache it');
                //Call back with the original URL in the hope they're online
                callback(url);
            });
        });

    }
tomcooksey commented 9 years ago

Further to this, I discovered that this seems to happen much more often with large filesizes. The ones that were failing were 2.1mb. When I switched on compression for these images they are cached far more reliably..is there a limit to the filsize? I tried to find a limit but have struggled.

chrisben commented 9 years ago

ImgCache doesn't limit the number of files to cache or the size of those files. It relies on the Cordova File Plugin for the downloading part. Perhaps this plugin has its own limitations, I don't know. Perhaps it's the available space on the device, or the iOS implementation of the file plugin buffers everything in memory instead of the disk and the ipad runs out of memory, which kills the download thread...

Perhaps you could try to download a large file directly with the cordova file plugin (look at the implementation of ImgCache for an example) to see what's going on there, I'm sorry I won't be able to do this test for you at the moment.

chrisben commented 9 years ago

@tomcooksey did you find out what limitation exists for image file sizes, and the reason for it?

Blossicot commented 9 years ago

'ERROR: Download error code: 3' I also had a similar error. It works now by adding "cordova-plugin-whitelist".

cordova plugin add cordova-plugin-whitelist otherwise http requests are blocked - as of the latest Cordova version.