2010-12-24 23:50:42 +08:00
|
|
|
/*
|
|
|
|
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
|
|
|
|
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2010, Nitobi Software Inc.
|
|
|
|
* Copyright (c) 2010, IBM Corporation
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-01-06 22:57:54 +08:00
|
|
|
* FileTransfer uploads a file to a remote server.
|
2010-12-24 23:50:42 +08:00
|
|
|
*/
|
2011-01-06 22:57:54 +08:00
|
|
|
function FileTransfer() {};
|
2010-12-24 23:50:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* FileUploadResult
|
|
|
|
*/
|
|
|
|
function FileUploadResult() {
|
|
|
|
this.bytesSent = 0;
|
|
|
|
this.responseCode = null;
|
|
|
|
this.response = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-01-06 22:57:54 +08:00
|
|
|
* FileTransferError
|
2010-12-24 23:50:42 +08:00
|
|
|
*/
|
2011-01-06 22:57:54 +08:00
|
|
|
function FileTransferError() {
|
2010-12-24 23:50:42 +08:00
|
|
|
this.code = null;
|
|
|
|
};
|
|
|
|
|
2011-01-06 22:57:54 +08:00
|
|
|
FileTransferError.FILE_NOT_FOUND_ERR = 1;
|
|
|
|
FileTransferError.INVALID_URL_ERR = 2;
|
|
|
|
FileTransferError.CONNECTION_ERR = 3;
|
2010-12-24 23:50:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an absolute file path, uploads a file on the device to a remote server
|
|
|
|
* using a multipart HTTP request.
|
|
|
|
* @param filePath {String} Full path of the file on the device
|
|
|
|
* @param server {String} URL of the server to receive the file
|
|
|
|
* @param successCallback (Function} Callback to be invoked when upload has completed
|
|
|
|
* @param errorCallback {Function} Callback to be invoked upon error
|
|
|
|
* @param options {FileUploadOptions} Optional parameters such as file name and mimetype
|
|
|
|
*/
|
2011-01-06 22:57:54 +08:00
|
|
|
FileTransfer.prototype.upload = function(filePath, server, successCallback, errorCallback, options) {
|
2010-12-24 23:50:42 +08:00
|
|
|
|
|
|
|
// check for options
|
|
|
|
var fileKey = null;
|
|
|
|
var fileName = null;
|
|
|
|
var mimeType = null;
|
|
|
|
var params = null;
|
|
|
|
if (options) {
|
|
|
|
fileKey = options.fileKey;
|
|
|
|
fileName = options.fileName;
|
|
|
|
mimeType = options.mimeType;
|
|
|
|
if (options.params) {
|
|
|
|
params = options.params;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
params = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-06 22:57:54 +08:00
|
|
|
PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params]);
|
2010-12-24 23:50:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options to customize the HTTP request used to upload files.
|
|
|
|
* @param fileKey {String} Name of file request parameter.
|
|
|
|
* @param fileName {String} Filename to be used by the server. Defaults to image.jpg.
|
|
|
|
* @param mimeType {String} Mimetype of the uploaded file. Defaults to image/jpeg.
|
|
|
|
* @param params {Object} Object with key: value params to send to the server.
|
|
|
|
*/
|
|
|
|
function FileUploadOptions(fileKey, fileName, mimeType, params) {
|
|
|
|
this.fileKey = fileKey || null;
|
|
|
|
this.fileName = fileName || null;
|
|
|
|
this.mimeType = mimeType || null;
|
|
|
|
this.params = params || null;
|
|
|
|
};
|