I’m having trouble uploading an image using the FileTransfer cordova plugin.
If I use the code below I get an error code 1 result
//this will be called first
getImage()
{
var callback = (function (imageUri) { this._imageUri = imageUri }).bind(this);
navigator.camera.getPicture(function cameraSuccess(imageUri) {
callback(imageUri);
}, function cameraError(error) {
console.debug("Unable to obtain picture: " + error, "app");
}, {
quality: 50,
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
}
//after getImage is called this will be called
uploadImage()
{
this._platform.ready().then(() => {
var win = function (r) {
alert("Completed");
console.debug("Code = " + r.responseCode);
console.debug("Response = " + r.response);
console.debug("Sent = " + r.bytesSent);
}
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.debug("upload error source " + error.source);
console.debug("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = this._imageUri.substr(this._imageUri.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var ft = new FileTransfer();
ft.upload(this._imageUri, encodeURI("apiurl"), win, fail, options);
});
}
If I use the code below I can hit the api.
This is using input element type=file to select the file.
When I hit the api the file is not sent, this is because var filename = (<HTMLInputElement>document.getElementById("uploadFile")).value;
will not get the actual file path.
ngAfterViewInit()
{
var callback = (function (imageUri) { this._imageUri = imageUri }).bind(this);
document.getElementById("uploadFile").onchange = function () {
var filename = (<HTMLInputElement>document.getElementById("uploadFile")).value;
callback(filename);
}
}
// will call this method after a file is selected
uploadImage()
{
this._platform.ready().then(() => {
var win = function (r) {
alert("Completed");
console.debug("Code = " + r.responseCode);
console.debug("Response = " + r.response);
console.debug("Sent = " + r.bytesSent);
}
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.debug("upload error source " + error.source);
console.debug("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = this._imageUri.substr(this._imageUri.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var ft = new FileTransfer();
ft.upload(this._imageUri, encodeURI("apiurl"), win, fail, options);
});
}
I hope somebody can help me with this.