Presentation is loading. Please wait.

Presentation is loading. Please wait.

Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.

Similar presentations


Presentation on theme: "Phonegap Bridge – File System CIS 136 Building Mobile Apps 1."— Presentation transcript:

1 Phonegap Bridge – File System CIS 136 Building Mobile Apps 1

2 File Transfer Files API 2

3 File Transfer API org.apache.cordova.file-transfer version 0.4.6 3  allows you to upload and download files  Protocol used:  HTTP multi-part POST request

4 File Transfer Object 4  Properties  onprogress:  Called with a ProgressEvent whenever a new chunk of data is transferred. (Function)  Methods:  upload: sends a file to a server  download: downloads a file from server  abort: Aborts an in-progress transfer

5 Upload 5

6 Upload() method 6 upload(URL, server, success, fail, options,trust);  fileURL: Filesystem URL representing the file on the device. For backwards compatibility, this can also be the full path of the file on the device.  server: URL of the server to receive the file, as encoded by encodeURI().  successCallback: A callback that is passed a Metadata object. (Function)  errorCallback: A callback that executes if an error occurs retrieving the Metadata. Invoked with a FileTransferError object. (Function)  options: Optional parameters (Object). Valid keys:  fileKey: The name of the form element. Defaults to file. (DOMString)  fileName: The file name to use when saving the file on the server. Defaults to image.jpg. (DOMString)  mimeType: The mime type of the data to upload. Defaults to image/jpeg. (DOMString)  params: A set of optional key/value pairs to pass in the HTTP request. (Object)  chunkedMode: Whether to upload the data in chunked streaming mode. Defaults to true. (Boolean)  headers: A map of header name/header values. Use an array to specify more than one value. (Object)  trustAllHosts: Optional parameter, defaults to false. If set to true, it accepts all security certificates. This is useful since Android rejects self-signed security certificates. Not recommended for production use.

7 Example Assumes variable fileURL contains a valid URL to a text file on the device, for example, cdvfile://localhost/persistent/path/to/file.txt 7 var options = new FileUploadOptions(); options.fileKey = "file"; options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1); options.mimeType = "text/plain"; var params = {}; params.value1 = "test"; params.value2 = "param"; options.params = params; var server=‘encodeURI("http://some.server.com/upload.php")’; var ftObj = new FileTransfer(); ftObj.upload(fileURL,server, success, fail, options);

8 Example (cont.) 8  Success callback will receive a FileUploadResult object with the following properties:  bytesSent: The number of bytes sent to the server as part of the upload. (long)  responseCode: The HTTP response code returned by the server. (long)  response: The HTTP response returned by the server. (DOMString)  headers: The HTTP response headers by the server. (Object) function success(r) { console.log("Code = " + r.responseCode); console.log("Response = " + r.response); console.log("Sent = " + r.bytesSent ); } function fail(error) { alert("An error has occurred: Code = " + error.code); console.log("upload error source " + error.source); console.log(“ upload error target " + error.target ); }

9 Download 9

10 download() method 10 download(server, fileURL, success, fail,trust,options);  fileURL: Filesystem URL representing the file on the device. For backwards compatibility, this can also be the full path of the file on the device  server: URL of the server to receive the file, as encoded by encodeURI()  successCallback: A callback that is passed a FileEntry object. (Function)  errorCallback: A callback that executes if an error occurs retrieving the Metadata. Invoked with a FileTransferError object. (Function)  options: Optional parameters (Object)  headers: A map of header name/header values. Use an array to specify more than one value. (Object)  trustAllHosts: Optional parameter, defaults to false. If set to true, it accepts all security certificates. This is useful since Android rejects self- signed security certificates.

11 11 var fileTransfer = new FileTransfer(); var server = encodeURI("http://some.server.com/download.php"); var options = ‘headers: { "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="}’ fileTransfer.download(server, fileURL, success, fail, false, options); function success(entry) { console.log("download complete: " + entry.toURL()); } function fail (error) { console.log("download error source " + error.source); console.log("download error target " + error.target); console.log("upload error code" + error.code); } Example Assumes variable fileURL contains a valid URL to a text file on the device, for example, cdvfile://localhost/persistent/path/to/file.txt

12 Abort 12

13 Abort 13  Aborts an in-progress transfer  The onerror callback is passed a FileTransferError object  has an error code of FileTransferError.ABORT_ERR (4)

14 FileTransferError Object 14  A FileTransferError object is passed to an error callback when an error occurs.  Properties  code: One of the predefined error codes listed below. (Number)  source: URL to the source. (String)  target: URL to the target. (String)  http_status: HTTP status code. This attribute is only available when a response code is received from the HTTP connection. (Number)  exception: Either e.getMessage or e.toString (String)  Constants  1 = FileTransferError.FILE_NOT_FOUND_ERR  2 = FileTransferError.INVALID_URL_ERR  3 = FileTransferError.CONNECTION_ERR  4 = FileTransferError.ABORT_ERR  5 = FileTransferError.NOT_MODIFIED_ERR

15 Example Assumes variable fileURL contains a valid URL to a text file on the device, for example, cdvfile://localhost/persistent/path/to/file.txt 15 var options = new FileUploadOptions(); options.fileKey="file"; options.fileName="myphoto.jpg"; options.mimeType="image/jpeg"; Var server = ‘encodeURI("http://some.server.com/upload.php"), ’ var ftObj = new FileTransfer(); ftObj.upload(fileURL, server, success, fail, options); ftObj.abort(); function success(r) { console.log("Should not be called."); } function fail(error) { // error.code == FileTransferError.ABORT_ERR alert("An error has occurred: Code = " + error.code); console.log("upload error source " + error.source); console.log("upload error target " + error.target); }


Download ppt "Phonegap Bridge – File System CIS 136 Building Mobile Apps 1."

Similar presentations


Ads by Google