in www/browser/FileTransfer.js [94:220]
FileTransfer.prototype.upload = function (filePath, server, successCallback, errorCallback, options) {
// check for arguments
argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
// Check if target URL doesn't contain spaces. If contains, it should be escaped first
// (see https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md#upload)
if (!checkURL(server)) {
if (errorCallback) {
errorCallback(new FileTransferError(FileTransferError.INVALID_URL_ERR, filePath, server));
}
return;
}
options = options || {};
const fileKey = options.fileKey || 'file';
const fileName = options.fileName || 'image.jpg';
const mimeType = options.mimeType || 'image/jpeg';
const params = options.params || {};
const withCredentials = options.withCredentials || false;
// var chunkedMode = !!options.chunkedMode; // Not supported
const headers = options.headers || {};
const httpMethod = options.httpMethod && options.httpMethod.toUpperCase() === 'PUT' ? 'PUT' : 'POST';
const basicAuthHeader = getBasicAuthHeader(server);
if (basicAuthHeader) {
server = server.replace(getUrlCredentials(server) + '@', '');
headers[basicAuthHeader.name] = basicAuthHeader.value;
}
const that = this;
const xhr = (transfers[this._id] = new XMLHttpRequest());
xhr.withCredentials = withCredentials;
const fail =
errorCallback &&
function (code, status, response) {
if (transfers[this._id]) {
delete transfers[this._id];
}
const error = new FileTransferError(code, filePath, server, status, response);
if (errorCallback) {
errorCallback(error);
}
};
window.resolveLocalFileSystemURL(
filePath,
function (entry) {
entry.file(
function (file) {
const reader = new FileReader();
reader.onloadend = function () {
const blob = new Blob([this.result], { type: mimeType });
// Prepare form data to send to server
const fd = new FormData();
fd.append(fileKey, blob, fileName);
for (const prop in params) {
if (Object.prototype.hasOwnProperty.call(params, prop)) {
fd.append(prop, params[prop]);
}
}
xhr.open(httpMethod, server);
// Fill XHR headers
for (const header in headers) {
if (Object.prototype.hasOwnProperty.call(headers, header)) {
xhr.setRequestHeader(header, headers[header]);
}
}
xhr.onload = function () {
// 2xx codes are valid
if (this.status >= 200 && this.status < 300) {
const result = new FileUploadResult();
result.bytesSent = blob.size;
result.responseCode = this.status;
result.response = this.response;
delete transfers[that._id];
successCallback(result);
} else if (this.status === 404) {
fail(FileTransferError.INVALID_URL_ERR, this.status, this.response);
} else {
fail(FileTransferError.CONNECTION_ERR, this.status, this.response);
}
};
xhr.ontimeout = function () {
fail(FileTransferError.CONNECTION_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) {
if (that.onprogress) {
that.onprogress(e);
}
};
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 (!transfers[that._id]) {
xhr.abort();
}
};
reader.readAsArrayBuffer(file);
},
function () {
fail(FileTransferError.FILE_NOT_FOUND_ERR);
}
);
},
function () {
fail(FileTransferError.FILE_NOT_FOUND_ERR);
}
);
};