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

Failed to cache files on local file system #85

Open confile opened 9 years ago

confile commented 9 years ago

I tried to cache a file with is on my iPhone 5 iOS 7.1.1 at:

/var/mobile/Applications/220B3516-0877-4A8C-AF84-DC05E4F2F051/Documents/ImageFile-349402272.png

Here is what I get in the debug console having debug = true:

ERROR: Download error source: /var/mobile/Applications/220B3516-0877-4A8C-AF84-DC05E4F2F051/Documents/ImageFile-349402272.png
ERROR: Download error target: file:///var/mobile/Applications/220B3516-0877-4A8C-AF84-DC05E4F2F051/Documents/imgcache//044eaad7240479f25687a16bf91dfe3b9a602ece.png
ERROR: Download error code: 3

There is a url in the cache folder but the caching did fail. I did isCached and it returned false.

BrunoCartier commented 9 years ago

Hi,

Without giving the code you use to generate this error, I doubt anybody will be able to help you correctly ;)

confile commented 9 years ago

Here is the code:

var url = "/var/mobile/Applications/220B3516-0877-4A8C-AF84-DC05E4F2F051/Documents/ImageFile-349402272.png";
ImgCache.init(function(){
    alert('ImgCache init: success!');

 ImgCache.cacheFile(url);
}, function(){
    alert('ImgCache init: error! Check the log for errors');
});
BrunoCartier commented 9 years ago

I'm not very familiar with the iOS platform, but the problem seems to "simply" be that it's not possible to access (GET) your image, either because it doesn't exist, or there is a access permission issue.

Make a HTTP request targeting the URI of your image, then log the result of this request. The error code will probably help. Also, you should see the doc for the File Plugin in iOS, because to access a file, if I'm not mistaken, the protocol is something special, like file:// or cdvfile://.

confile commented 9 years ago

I can access the image. I put it in a tag and it gets displayed. So the image exists.

I also have this in my config.xml

    <platform name="android">
        <preference name="AndroidPersistentFileLocation" value="Internal" />
    </platform>

    <platform name="ios">
        <preference name="iosPersistentFileLocation" value="Library" />
    </platform>
BrunoCartier commented 9 years ago

Which Cordova version are you using? It seems that this bug has been found in 3.3 & 3.4.

confile commented 9 years ago

I use the latest version 4.1.2.

confile commented 9 years ago

The bug you mentioned is from Android. I am working on iOS. I think the problem is that I want to cache a file which is still on the local file system of the device. This means that requesting it with a HTTP GET might not work.

I think that imgcache.js should check if the imageUrl to be cached starts with

/var

if this is the case it should not use HTTP GET instead it should use the file api to access the file.

chrisben commented 9 years ago

ImgCache is not meant to cache files that don't need to be cached (local files) so I don't expect it to work in that case. It is meant to cache online files (off the web) in case you get offline.

@confile , what were you trying to achieve there?

confile commented 9 years ago

I try to copy the local image file fr the library in the cache folder. In case the user deletes the file from the library I would still have a copy in the cache to be used.

How would you do that?

chrisben commented 9 years ago

I suggest you have a look at the Cordova File plugin directly. Then you could implement something like this I guess.

chrisben commented 9 years ago

@confile any news on that? v1.0rc1 is out now, it includes a fix for paths with iOS cordova targets. Let me know if that fixes your problem.

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.

maximnara commented 3 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.

Thank's! This is worked for me on android device. Also need to make right link on local file.