cordova-android/framework/assets/js/filetransfer.js

112 lines
3.9 KiB
JavaScript
Raw Normal View History

2010-12-24 23:50:42 +08:00
/*
2011-10-28 05:04:39 +08:00
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
2011-10-28 05:04:39 +08:00
* 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.
2010-12-24 23:50:42 +08:00
*/
if (!PhoneGap.hasResource("filetransfer")) {
PhoneGap.addResource("filetransfer");
2010-12-24 23:50:42 +08:00
/**
2011-01-06 22:57:54 +08:00
* FileTransfer uploads a file to a remote server.
* @constructor
2010-12-24 23:50:42 +08:00
*/
var FileTransfer = function() {};
2010-12-24 23:50:42 +08:00
/**
* FileUploadResult
* @constructor
2010-12-24 23:50:42 +08:00
*/
var FileUploadResult = function() {
2010-12-24 23:50:42 +08:00
this.bytesSent = 0;
this.responseCode = null;
this.response = null;
};
2010-12-24 23:50:42 +08:00
/**
2011-01-06 22:57:54 +08:00
* FileTransferError
* @constructor
2010-12-24 23:50:42 +08:00
*/
var FileTransferError = function() {
2010-12-24 23:50:42 +08:00
this.code = null;
};
2010-12-24 23:50:42 +08:00
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
2010-12-24 23:50:42 +08:00
* 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
2010-12-24 23:50:42 +08:00
*/
FileTransfer.prototype.upload = function(filePath, server, successCallback, errorCallback, options, debug) {
2010-12-24 23:50:42 +08:00
// check for options
var fileKey = null;
var fileName = null;
var mimeType = null;
var params = null;
var chunkedMode = true;
2010-12-24 23:50:42 +08:00
if (options) {
fileKey = options.fileKey;
fileName = options.fileName;
mimeType = options.mimeType;
2011-11-08 06:54:15 +08:00
if (options.chunkedMode !== null || typeof options.chunkedMode !== "undefined") {
chunkedMode = options.chunkedMode;
}
2010-12-24 23:50:42 +08:00
if (options.params) {
params = options.params;
}
else {
params = {};
}
}
PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, debug, chunkedMode]);
2010-12-24 23:50:42 +08:00
};
2011-11-27 01:16:44 +08:00
/**
* Downloads a file form a given URL and saves it to the specified directory.
* @param source {String} URL of the server to receive the file
* @param target {String} Full path of the file on the device
2011-11-27 01:16:44 +08:00
* @param successCallback (Function} Callback to be invoked when upload has completed
* @param errorCallback {Function} Callback to be invoked upon error
*/
FileTransfer.prototype.download = function(source, target, successCallback, errorCallback) {
PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'download', [source, target]);
2011-11-27 01:16:44 +08:00
};
2010-12-24 23:50:42 +08:00
/**
* Options to customize the HTTP request used to upload files.
* @constructor
2010-12-24 23:50:42 +08:00
* @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) {
2010-12-24 23:50:42 +08:00
this.fileKey = fileKey || null;
this.fileName = fileName || null;
this.mimeType = mimeType || null;
this.params = params || null;
};
}