ngCordova FileTransferPlugin error

Hello guys,

I am creating an Ionic App which transfer an image to an server.
I am using the FileTransferPlugin, but it throws an error and says FileTransfer is not defined:

here is my Controllercode:

angular.module(‘starter’, [‘ionic’,‘ngCordova’])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})

.controller(‘CameraCtrl’, function($scope, $cordovaFileTransfer) {
$scope.upload = function(){
var options = {
fileKey: “avatar”,
fileName: “profilpic.jpg”,
chunkedMode: false,
mimeType: “image/jpg”
};
$cordovaFileTransfer.upload(“http://10.0.1.139:5000/upload”, “…/img/profilpic.jpg”, options, true).then(function(result) {
console.log("SUCCESS: " + JSON.stringify(result.response));
}, function(err) {
console.log("ERROR: " + JSON.stringify(err));
}, function (progress) {
// constant progress updates
});
}

})

please help me!

Check out the troubleshooting tips here: https://stackoverflow.com/questions/28929441/filetransfer-not-defined-angularjs

thanks, but that didnt helped me,
i wrote deviceready in the app, but it isnt fired :confused:

do you added the plugin at $cordova add plugin File and FileTransfer?

Yes, the file-Plugin works.
i installed booth with the cli-command

You don’t have to inject a $cordovaFileTransfer if you properly added the plugin to your ionic project, just create a new FileTransfer object and call upload from there like in the documentation example for the cordova file transfer plugin.

not

$cordovaFileTransfer.upload("http://10.0.1.139:5000/upload", "../img/profilpic.jpg", options, true).then(function(result) {
    console.log("SUCCESS: " + JSON.stringify(result.response));
}, function(err) {
    console.log("ERROR: " + JSON.stringify(err));
}, function (progress) {
    // constant progress updates
});

but this

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("http://some.server.com/upload.php");

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

var headers={'headerParam':'headerValue'};

options.headers = headers;

var ft = new FileTransfer();
ft.onprogress = function(progressEvent) {
    if (progressEvent.lengthComputable) {
      loadingStatus.setPercentage(progressEvent.loaded / progressEvent.total);
    } else {
      loadingStatus.increment();
    }
};
ft.upload(fileURL, uri, win, fail, options);
1 Like

thansk you helped me a lot.