function successCB(entry) {
if (entry.isFile) {
- entry.file(function(file) {
- function uploadFile(blobFile) {
- var fd = new FormData();
+ // initialize XMLHTTPRequest (without starting it)
+ var xhr = uploads[id] = new XMLHttpRequest();
- fd.append(fileKey, blobFile, fileName);
+ xhr.open(httpMethod, server);
- for (var prop in params) {
- if(params.hasOwnProperty(prop)) {
- fd.append(prop, params[prop]);
- }
- }
- var xhr = uploads[id] = new XMLHttpRequest();
+ // Fill XHR headers
+ for (var header in headers) {
+ if (headers.hasOwnProperty(header)) {
+ xhr.setRequestHeader(header, headers[header]);
+ }
+ }
- xhr.open(httpMethod, server);
+ xhr.ontimeout = function(evt) {
+ fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
+ };
- // Fill XHR headers
- for (var header in headers) {
- if (headers.hasOwnProperty(header)) {
- xhr.setRequestHeader(header, headers[header]);
- }
- }
+ xhr.onerror = function() {
+ fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
+ };
- xhr.onload = function(evt) {
- if (xhr.status === 200) {
- uploads[id] && delete uploads[id];
- successCallback({
- bytesSent: file.size,
- responseCode: xhr.status,
- response: xhr.response
- });
- } else if (xhr.status === 404) {
- fail(FileTransferError.INVALID_URL_ERR, this.status, this.response);
- } else {
- fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
- }
- };
-
- xhr.ontimeout = function(evt) {
- fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
- };
+ xhr.onabort = function () {
+ fail(FileTransferError.ABORT_ERR, this.status, this.response);
+ };
- xhr.onerror = function() {
- fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
- };
-
- xhr.onabort = function () {
- fail(FileTransferError.ABORT_ERR, this.status, this.response);
- };
+ xhr.upload.onprogress = function (e) {
+ successCallback(e);
+ };
+ // end of XMLHTTPRequest initialization
- xhr.upload.onprogress = function (e) {
- successCallback(e);
- };
+ var fullPath = entry.toURL();
+ var fileHandle;
- xhr.send(fd);
+ function closeHandle() {
+ if (fileHandle) {
+ fileHandle.close();
+ }
+ }
- // Special case when transfer already aborted, but XHR isn't sent.
- // In this case XHR won't fire an abort event, so we need to check if transfers record
- // isn't deleted by filetransfer.abort and if so, call XHR's abort method again
- if (!uploads[id]) {
- xhr.abort();
+ function uploadFile(blobFile) {
+ closeHandle();
+ // create FormData
+ var fd = new FormData();
+ fd.append(fileKey, blobFile, fileName);
+ for (var prop in params) {
+ if(params.hasOwnProperty(prop)) {
+ fd.append(prop, params[prop]);
}
}
- uploadFile(file);
+ // sending already initialized request
+ // 'onload' needs to be defined here because it needs blobFile.size
+ xhr.onload = function(evt) {
+ // 2xx codes are valid
+ if (xhr.status >= 200 && xhr.status < 300) {
+ uploads[id] && delete uploads[id];
+ successCallback({
+ bytesSent: blobFile.size,
+ responseCode: xhr.status,
+ response: xhr.response
+ });
+ } else if (xhr.status === 404) {
+ fail(FileTransferError.INVALID_URL_ERR, this.status, this.response);
+ } else {
+ fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
+ }
+ };
+ xhr.send(fd);
+
+ // Special case when transfer already aborted, but XHR isn't sent.
+ // In this case XHR won't fire an abort event, so we need to check if transfers record
+ // isn't deleted by filetransfer.abort and if so, call XHR's abort method again
+ if (!uploads[id]) {
+ xhr.abort();
+ }
+ }
- }, function(error) {
- fail(FileTransferError.CONNECTION_ERR);
- });
+ try {
+ fileHandle = tizen.filesystem.openFile(fullPath, 'r');
+ var fileBlob = fileHandle.readBlobNonBlocking(
+ uploadFile,
+ function(error) {
+ closeHandle();
+ fail(FileTransferError.ABORT_ERR, 'Could not read file ' + e);
+ }
+ );
+ } catch (e) {
+ closeHandle();
+ fail(FileTransferError.ABORT_ERR, 'Could not read file ' + e);
+ }
}
}