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!