Search code examples
angularjscordovaasp.net-web-apingcordova

Excetipn in file uploading using ngCordova


I'm working on a cross platform mobile app using cordova with backend server written in Asp.net I want to upload a file to the server from the application using REST api . I just wanted to check if the method will be invoked using a break point. I get an error with my link as the exception message. It doesn't upload and the break point doesn't even start . Here is the code

[HttpPost]
        [Route("Api/Token/test")]
        public IHttpActionResult TestFile(HttpPostedFileBase file) {
            if (file != null)
            {

            }
            return Ok();
        }`

this my code to upload the file

var url = "http:/192.168.101.1:3000/api/token/test";
//File for Upload
 var targetPath = cordova.file.externalRootDirectory + "logo_radni.png";
// File name only
var filename = targetPath.split("/").pop();
var options = {
   fileKey: "file",
    fileName: filename,
    chunkedMode: false,
    mimeType: "image/jpg",
   params : {'directory':'upload', 'fileName':filename} 
};

$cordovaFileTransfer.upload(url, targetPath,options).then(function (result) {
console.log("SUCCESS: " + JSON.stringify(result.response));
                     }, function (err) {
                         console.log("ERROR: " + JSON.stringify(err));
                     }, function (progress) {
                         // PROGRESS HANDLING GOES HERE
                     });

this is the output from the console {"code":3,"source":"file:///storage/emulated/0/logo_radni.png","target":"http:/192.168.101.1:3000/api/token/test","http_status":null,"body":null,"exception":"http:/192.168.101.1:3000/api/token/test"} (21:47:56:267)

This is my config.xml

 <access origin="*"/>
    <allow-intent href="http://*/*"/>
    <allow-intent href="https://*/*"/>
    <allow-intent href="tel:*"/>
    <allow-intent href="sms:*"/>
    <allow-intent href="mailto:*"/>
    <allow-intent href="geo:*"/>
    <allow-navigation href="http://*/*" />
    <allow-navigation href="https://*/*" />

Solution

  • I believe you are missing a slash in http:/192.168.101.1:3000 where it should be http://192.168.101.1:3000 .