File Transfer using PhoneGap

The FileTransfer object lets you upload files to a remote server using an HTTP multi-part POST request. Both HTTP and HTTPS protocols are supported. You can also specify optional parameters with the FileUploadOptions object. When you successfully upload a file, the results are stored in a FileUploadResult object. All errors are stored in a FileTransferError object.

Following is an example: 

function success(f) {
    alert("Code = " + f.responseCode);
    alert(" Response = " + f.response);
    alert("Sent = " + f.bytesSent);
   }
function fail(error) {
    alert("An error has occurred: Code = " = error.code);
    }
var upload_options = new FileUploadOptions();
    upload_options.fileKey = "file";
    upload_options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
    upload_options.mimeType = "text/plain";
var params = new Object();
    params.value1 = "sample text";
    params.value2 = "another sample";
    upload_options.params = params;
var ft = new FileTransfer();
    ft.upload(fileURI, "http://some.server.com/upload.php", success, fail, options);

FileUploadOptions

As mentioned,a FileUploadOption object can be passed to the FileTransfer objects upload method in order to specify additional parameters to upload script. We can pass along a set of options when you perform a file upload. These options are stored in a FileUploadOptions object.
The FileUploadOptions object has the following properties.

  • fileKey : This is the name of the form element and if you will not set then by default it make it to file.
  • fileName : This is the filename that  you want the file to be saved as on the server and if not set then by default it make it to image.jpg.
  • mimeType : This is the MIME type of data that you are uploading and if it not set then by default it make it to image/jpeg.
  • params : This is a set of the optional key pairs you can pass along in the HTTP request.

FileUploadResults

When you successfully upload a file, the results are stored in a FileUploadResult object.
The FileUploadResult object has the following properties.

  • bytesSent : This is the number of bytes sent to the server as part of the upload.
  • responseCode : This is the HTTP response code returned by the server.
  • response : This is the HTTP response returned by the server.   
FileTransferError
 

A FileTransferError object is returned via the error callback when an error occurs. It contains one property, code, which is one of the following predefined codes:

  • FileTransferError.FILE_NOT_FOUND_ERR.
  • FileTransferError.INVALID_URL_ERR.
  • FileTransferError.CONNECTION_ERR.

Supported Platform for Beep and Vibrations

  • Android
  • BlackBerry (OS 4.6)
  • BlackBerry WebWorks (OS 5.0 and higher)
  • iPhone
  • Windows Phone 7 ( Mango ) 

Resources

Some of the useful resources are as follows.

Filesystem-Reading Files in PhonGap
Filesystem-Writing Files in PhonGap
FileSystem FileEntry Objects Using PhoneGap
FileSystem-DirectoryEntry Objects in PhoneGap

Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles