3 Replies Latest reply on Jun 25, 2017 3:59 AM by Speedpiano

    File-upload error using Cordova FileTransfer to NodeJS

    Speedpiano

      I'm currently working on a Phonegap app, and I would like users to be able to upload any file to my NodeJS server.

      I've looking all around the web but I just can't get anything to work...

      Here is the code I'm using for the Phonegap controller:

      $scope.open = function()
      {
        navigator
      .camera.getPicture(upload,
        
      function(message)
        
      {
        alert
      ('get picture failed');
        
      },
        
      {
        quality
      : 50,
        destinationType
      : navigator.camera.PictureSourceType.FILE_URI,
        sourceType
      : navigator.camera.PictureSourceType.PHOTOLIBRARY,
        mediaType
      : navigator.camera.MediaType.ALLMEDIA 
        
      });

        
      }

        
      var win = function (r) {
        $scope
      .log = "Code = " + r.responseCode;
        $scope
      .log2 = "Response = " + r.response;
        $scope
      .log3 = "Sent = " + r.bytesSent;
        $scope
      .$digest(); 

        
      }

        
      var fail = function (error) {
        $scope
      .log = "An error has occurred: Code = " + error.code;
        $scope
      .log2 = "upload error source " + error.source;
        $scope
      .log3 = "upload error target " + error.target;
        $scope
      .$digest(); 
        
      }

        
      function upload(fileURI)
        
      {
        $scope
      .log = fileURI;
        $scope
      .$digest(); 

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

        
      var params = {};
        params
      .value1 = "test";
        params
      .value2 = "param";
        options
      .params = params;

        
      var ft = new FileTransfer();
        ft
      .upload(fileURI, "http://192.168.192.111:2999/upload", win, fail, options);
         }

       

      Here is the current code for the NodeJS server, have tried a lot of different things, all without success:

      var express = require('express');
      var http = require('http').Server(express);
      var io = require('socket.io')(http);
      var fs = require('fs');

      var multer = require('multer');
      var app = new express();


      app
      .post('/upload', multer({dest: './uploads/'}).single('upl'), function(req, res)
      {
        console
      .log(req.body);
        console
      .log(req.file);
      })


      http
      .listen(2999, function(){
        console
      .log('listening on *:2999');
      });

      In the app I used to get errors that FileUploadOptions etc weren't defined, but I fixed that by adding them to the cordova project. Furthermore, I use ionic 1, if that helps anyone out.

      I do keep constantly getting the error code 1 (upload error source), even though I selected a real file and I saw that the link was correct (something like /storage/0/emulated/Downloads on my Android device).

      Also, sometimes it gives me error 3 as well (upload target source), some sort of server not found issue I think.

      Is there something obvious I'm doing wrong and how would I be able to fix it? Is there a handier way, since I eventually want to link this to a MySQL database.

      Thanks in advance!

        • 1. Re: File-upload error using Cordova FileTransfer to NodeJS
          kerrishotts Adobe Community Professional

          Error code 1 indicates that the file you wanted to upload was not found. Verify that the file exists at the path you are using AND that your app has the appropriate permissions to access the file.

           

          Error code 3 indicates a connection error. Make sure that your server is accessible from your device, and that the server isn't rejecting your device's requests (check the firewall, for example).

           

          I'd use `adb logcat` to see more about what's going on while your app tries to upload the files -- it might point out some useful information as to why the attempt is failing.

          1 person found this helpful
          • 2. Re: File-upload error using Cordova FileTransfer to NodeJS
            Speedpiano Level 1

            Well I tried a few things, first off I tried changing only the server to http://posttestserver.com/post.php (a test server, see Henry's HTTP Post Dumping Server )

             

            This worked exactly as it should, so it was a server thing. Then I thought let's try it with PHP, and it didn't work either. Turning of my firewall did do the trick though. So now uploading works using PHP, I have no clue why it would first give me error 1, the file was found anyway.

             

            Then I also have no clue as to why it doesn't work with NodeJS.

             

            Unfortunately I have a problem with downloading as well, this is my code for that:

             

            var uri = encodeURI("http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf");

            var target = "D:/File.pdf"

                    var ft = new FileTransfer();

                    ft.download(

                        uri,

                        target,

                        function(entry) {

                            console.log("success");

                            console.log("download complete: " + entry.fullPath);

             

             

                        },

                        function(err) {

                            console.log(err);

                            console.log(JSON.stringify(err));

                        },

                        true,

                        {

                            headers: {}

                        }

                    );

             

                

            I used this uri because that is a file I'm sure exists. Furthermore, in the config xml I have these plugins setup:

             

                <plugin name="cordova-plugin-console" spec="~1.0.3" />

                <plugin name="cordova-plugin-device" spec="~1.1.2" />

                <plugin name="cordova-plugin-splashscreen" spec="~3.2.2" />

                <plugin name="cordova-plugin-statusbar" spec="~2.1.3" />

                <plugin name="cordova-plugin-whitelist" spec="~1.2.2" />

                <plugin name="ionic-plugin-keyboard" spec="~2.2.0" />

                <plugin name="cordova-plugin-camera" spec="~2.1.1" />

                <plugin name="cordova-plugin-file" spec="~4.3.2" />

                <plugin name="cordova-plugin-file-transfer" spec="~1.5.1" />

             

            The Android manifest has permissions setup:

             

                <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

                <uses-permission android:name="android.permission.READ_CONTACTS" />

                <uses-permission android:name="android.permission.WRITE_CONTACTS" />

                <uses-permission android:name="android.permission.GET_ACCOUNTS" />

                <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

                <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

                <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

                <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

                <uses-permission android:name="android.permission.RECORD_AUDIO" />

                <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

                <uses-permission android:name="android.permission.READ_PHONE_STATE" />

                <uses-permission android:name="android.permission.RECORD_VIDEO" />

                <uses-permission android:name="android.permission.VIBRATE" />

                <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

             

            I get this error:

            {"code":1,"source":"http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf","target":"D:/File.pdf","http_status":null,"body":null,"exception":null}

             

            I have no clue what to do now, as I think I have everything set up as it should be...

             

            The D:"/File.pdf is just for my PC, as that's easier to test than on my phone. When I change it to /storage/Downloads/Test.pdf or so it doesn't work either.

             

            I hope you could help me out!

            • 3. Re: File-upload error using Cordova FileTransfer to NodeJS
              Speedpiano Level 1

              Well I fixed it, it was again the issue that the target wasn't found... The target folder has to be 100% correct or it will give this error.

              So error 1 wasn't correct. This means that the errors are not completely relevant, unfortunately.