Ionic 2 with cordova-plugin-file-transfer upload error code 1

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.

I am struggling with the same problem. Android 6.0.1 Nexus 5.


I finally figured out the problem.
The error code 1 (FILE_NOT_FOUND_ERR) is misleading.
The issue here is not the file being uploaded but the api.
There was an issue with the api signature that’s why it’s not being recognized, kinda like a 404 error.
So try checking the api also.

Hope this helps everyone.

1 Like

Thank you so much. This was indeed server API issue. Appreciated.