| /* |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| */ |
| |
| if (!Cordova.hasResource("filetransfer")) { |
| Cordova.addResource("filetransfer"); |
| |
| /** |
| * FileTransfer uploads a file to a remote server. |
| * @constructor |
| */ |
| var FileTransfer = function() {}; |
| |
| /** |
| * FileUploadResult |
| * @constructor |
| */ |
| var FileUploadResult = function() { |
| this.bytesSent = 0; |
| this.responseCode = null; |
| this.response = null; |
| }; |
| |
| /** |
| * FileTransferError |
| * @constructor |
| */ |
| var FileTransferError = function() { |
| this.code = null; |
| }; |
| |
| FileTransferError.FILE_NOT_FOUND_ERR = 1; |
| FileTransferError.INVALID_URL_ERR = 2; |
| FileTransferError.CONNECTION_ERR = 3; |
| |
| /** |
| * 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 |
| */ |
| FileTransfer.prototype.upload = function(filePath, server, successCallback, errorCallback, options, debug) { |
| |
| // check for options |
| var params = null; |
| if (options) { |
| if (options.params) { |
| var dict=new Array(); |
| var idx = 0; |
| |
| for (var key in options.params) { |
| if (options.params.hasOwnProperty(key)) { |
| var value = options.params[key]; |
| var item = new Object(); |
| item.Key = key; |
| item.Value = value; |
| dict[idx] = item; |
| idx++; |
| } |
| } |
| |
| options.params = dict; |
| } |
| } else { |
| options = new FileUploadOptions(); |
| } |
| |
| options.filePath = filePath; |
| options.server = server; |
| |
| Cordova.exec(successCallback, errorCallback, 'FileTransfer', 'upload', options); |
| }; |
| |
| /** |
| * Options to customize the HTTP request used to upload files. |
| * @constructor |
| * @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. |
| */ |
| var FileUploadOptions = function(fileKey, fileName, mimeType, params) { |
| this.fileKey = fileKey || null; |
| this.fileName = fileName || null; |
| this.mimeType = mimeType || null; |
| this.params = params || null; |
| }; |
| } |