How to check if file exists locally otherwise download file and store locally, for PDFs & Videos

  • 1
  • Question
  • Updated 5 years ago
  • Doesn't Need an Answer
I have created an app with links to multiple PDFs & Video's using phonegap build, but I want to make the files accessible for offline use, eg... download all the PDFs & Videos onto the device, Android & iOS.

I want to have it so the user would click on a link & play it if it has been downloaded already or download it onto the device & then play it, possibly with some kind of progress indicator so the user knows it has been downloaded.

Any assistance would be extremely helpful,

Thanks

Andrew
Photo of Andrew

Andrew

  • 9 Posts
  • 0 Reply Likes
  • unsure

Posted 5 years ago

  • 1
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Well, apparantly you know in advance which files you want to make available, because you're able to create a list.
My take would be to create that list without caring for the exact location, first:
<ul>

<li><a href="Tutorial1.pdf" class="filelistitem">
<li><a href="Tutorial3.pdf" class="filelistitem">
<li><a href="SpecialMovie.mp4" class="filelistitem">
<li>...etc...
</ul>

Then add an eventListener for the click event for each list item's hyperlink, with a callback function (say: handleLink()). That's where the classname comes handy.

The callback function would:
- take the value from the clicked a.href
- check if that file exists locally (using the File plugin)
- if it does, display that file, otherwise use the File Transfer plugin to download the file from remote, and display it
- cancel the default click action for the a.href
Photo of Andrew

Andrew

  • 9 Posts
  • 0 Reply Likes
Thanks for the quick response Petra, I have read up on the file & filetransfer plugins but it looks so complex, this is my first app using phonegap so I'm a bit lost :(
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
I understand that. We've probably all been there...
You may want to play around with the file and filetransfer options and examples in a test app first, just to get a feel for the functions, parameters and callbacks. Then, when you have had the opportunity to go through that learning curve, pick up the 'real app' and implement those plugins.
I hope you're not stuck with a deadline...
Photo of Andrew

Andrew

  • 9 Posts
  • 0 Reply Likes
I'll go do that now, see how I go, thanks
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
And BTW, there are a couple of more situations you may want to consider:
1. What if the file is not present locally and the user has no network connection switched on? Do you want to display an error message, do you want to offer local alternatives? Anything else?
2. What if the user doesn't want to download large files, even though he has a network connection (for instance because he is on a monthly data transfer limit)? Wouldn't you like to offer a Settings switch which allows to user to switch automatic download from remote servers on/off? If so, you need to check that switch in your callback function as well.
Photo of Andrew

Andrew

  • 9 Posts
  • 0 Reply Likes
I have managed to get it downloading into the apps documents directory, but inappbrowser doesnt open them, I thought it might be because of the url starting with file:///var/ etc.... I am using this for the links

window.open(base_directory_path+current_download_file_name, '_system');

i've been seeing cdvfile://localhost/presistent googling but cant figure out what i am missing, any help would be great
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Not sure what the value of "base_directory_path+current_download_file_name" is in your case.
Also, I am not sure what "doesn't open" means. Are you getting a javascript error message? Is your inappbrowser plugin active at all? (In order to test the latter, you might try and open google.com in a '_blank' inappbrowser and see whether you get to see Google's site in a webview window or in a separate inappbrowser window)

Should the inappbrowser plugin be included correctly in your app, then the issue might be something with the way your pdf is stored or accessed using your file- and filetransfer plugins.
Would this be helpful?:
http://me.eekay.nl/index.php/2014/how...
Photo of Andrew

Andrew

  • 9 Posts
  • 0 Reply Likes
If I use this link window.open("http://Google.com", '_system'); it opens in safari

The base directory+file path is file:///var/....app/Documents/file.pdf etc...

No error message just nothing happens at all when using _system and linking to a file in local app documents folder, if target is _blank then it opens in inappbrowser popup with the done button at the bottom of window
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
if target is _blank then it opens in inappbrowser popup with the done button at the bottom of window
That all sounds pretty good.
What would happen if you copy the file url and open it in Safari (by enterung that url in the Safari address field)?

[I have never tried that myself, so I can't speak from experience. But....I wonder whether or not Safari has access to files in an app's own directory at all...if not, then that could explain why the document is shown in the _blank inappbrowser, but not in Safari].
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Oh, and I found this:
https://build.phonegap.com/plugins/1117
Haven't ever used that plugin, but it sounds promising.
Photo of Andrew

Andrew

  • 9 Posts
  • 0 Reply Likes
Yeah that's what I thought was probably happening, do you know of a storage location that is accessible to safari?
Photo of Andrew

Andrew

  • 9 Posts
  • 0 Reply Likes
I tried that file opener plugin, it brings up the open with... Popup, but safari isn't one of the options unfortunately
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Does it have to be Safari? I thought iOS would simply list the apps that could open such file (like the way Android works with intents).
Photo of Andrew

Andrew

  • 9 Posts
  • 0 Reply Likes
It could be ok for some things, but for example if you don t have a media player app then you have nothing to play video files
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
OK. Found a couple of others for you to try, if you wish:
http://plugins.cordova.io/#/package/c...
https://github.com/triceam/phonegap-p...
Photo of Pierre-Alex

Pierre-Alex

  • 1 Post
  • 0 Reply Likes
I guess it's pretty late to help you, but i post an answer for your last question in case someone runs in a similar issue.

iOS WebViews (i don't know about Android) can access iPhone local files by providing only the local path, it means without the 'file://'

So according to your example :
this : file:///var/....app/Documents/file.pdf
becomes : /var/....app/Documents/file.pdf

Hope this can help someone.