I've been working with the File API that is available from Cordova for working with photos and then uploading those images. I'm running into issues with the FileEntry class and also the FileTransfer class. Here are some code snippets, I've remarked out the FileTransfer call in this example:
var img = new Image();
img.style.display = 'block';
img.src = "http://localfile/Simulator////C:/Users/me/Pictures/bus.png";
var App = {
CachedPictures[{ID: 0, ImageBinary: img, ImageNm: "xx", ImageNotes: "xxxx"}],
SavePicture: function () {
for (var i = 0; i < App.CachedPictures.length; i++) {
var lastImage = App.CachedPictures[App.CachedPictures.length - 1];
var url = 'SomeURL?ImageNm=' + encodeURIComponent(lastImage.ImageNm) +
'&ImageNotes=' + encodeURIComponent(lastImage.ImageNotes);
// get instance of file object
var filePath = lastImage.ImageBinary.src.substr(lastImage.ImageBinary.src.lastIndexOf('///') + 3);
console.log("filePath:\t" + filePath);
var file = null;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function (fileSystem) {
fileSystem.root.getFile(filePath, { create: true, exclusive: false },
function (parent) {
console.log(parent);
},
function () {
console.log("** error getting file");
});
},
function (evt) {
console.log('** failed in file system request')
console.log(evt);
});
/* var imageURI = lastImage.ImageBinary.src;
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
console.log(options.fileName);
options.mimeType = "text/plain";
console.log('*** original image source');
console.log(lastImage.ImageBinary.src);
console.log('*** modified path');
console.log(options.fileName);
var ft = new FileTransfer();
ft.upload(imageURI, url,
function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
},
function (error) {
// error.code == FileTransferError.ABORT_ERR
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
},
options);
ft.abort();*/
}
}
}
When trying to use the file FileEntry class i get the following in my console.log call:
filePath: C:/Users/me/Pictures/bus.png App.js:152
** failed in file system request App.js:165
FileError {code: "Not Supported in Simulator"}
Is there a plan in the future to support the Cordova File APIs from the simulator?
Joe