Peter
Peter

Reputation: 1759

Cordova FileTransfer.upload file not found

I'm trying to upload a file with Cordova/Phonegap file-transfer-plugin like this:

function uploadFileFromMobile(fileURL, name, type) {
    function win(r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
    }

    function fail(error) {
        alert("An error has occurred: Code = " + error.code);
        console.log("upload error source " + error.source);
        console.log("upload error target " + error.target);
    }

    var uri = encodeURI(someURI);

    var options = new FileUploadOptions();
    options.fileKey = "file";
    //options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
    options.fileName = name;
    options.mimeType = type;

    options.headers = {
        'Accept': 'application/json',
        'httpMethod': 'POST'

    };
    options.chunkedMode = false;

    var ft = new FileTransfer();

    //fileURL = file:///storage/emulated/0/somefile.txt
    ft.upload(fileURL, uri, win, fail, options, true);

}

Thats what I get (from the fail function):

errorcode 1
upload error source file:///storage/emulated/0/DCIM/Camera/IMG_20161209_120808.jpg
upload error target https://my.url.com

I guess the path to my file is wrong, but I don't see why. Can anyone help me?

Upvotes: 2

Views: 1285

Answers (1)

Govind Singh
Govind Singh

Reputation: 11

Apply the following changes in your code

options.headers = {
        'Accept': 'application/json'
    };

options.httpMethod: 'POST';

Now your code will work properly.

Upvotes: 1

Related Questions