Book Image

PhoneGap 4 Mobile Application Development Cookbook

Book Image

PhoneGap 4 Mobile Application Development Cookbook

Overview of this book

Table of Contents (19 chapters)
PhoneGap 4 Mobile Application Development Cookbook
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Saving a file in the device storage


Thanks to the ability to traverse, read, and write to the device filesystem, an application can write a file to either a specific, predefined location or a location chosen by the user within the application.

How to do it...

We will allow the user to enter a remote URL for a file in a textbox to download and save that file in their mobile device:

  1. Firstly, create a new PhoneGap project named storage by running the following line:

    phonegap create storage com.myapp.storage storage
    
  2. Add the devices platform. You can choose to use Android, iOS, or both:

    cordova platform add ios
    cordova platform add android
    
  3. Add the file and file-transfer plugins by running this line:

    phonegap plugin add org.apache.cordova.file
    phonegap plugin add org.apache.cordova.file-transfer
    
  4. We're going to use the XUI JavaScript library to easily access the DOM elements, so we'll include the reference to the file within the head tag.

  5. Let's open www/index.html and clean up the unnecessary elements...