Tips for transferring an image to an FTP server using an FTP client without using multipart uploading methods

I am currently working on uploading an image to my FTP server. I have successfully retrieved the image and obtained its location at

file:///storage/sdcard0/Android/data/com.ionicframework.ftptranfer949961/cache/1467013143014.png

The image file I need to send is a .png file.

 var app = angular.module('starter', ['ionic','ngCordova'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})


app.service('MyService',function(){
  var imgPath = [];
  //var imgName = [];
  this.getImgPath = function(){
    return imgPath;
  }
  this.setImgPath = function(path){
    console.log(path);
    imgPath = path ;

  /*this.getImgName = function(){
    return imgName;
  }
  this.setImgName = function(pass){
    console.log(pass);
    imgName = pass;
  }*/

  }
});

//https://github.com/xfally/cordova-plugin-ftp

app.controller('mycontroller',function($scope,$cordovaCamera,$cordovaFileTransfer,MyService,$log,$timeout,$window){

  $scope.takePicture = function(){

    var options = { 
            quality : 75, 
            destinationType: Camera.DestinationType.FILE_URI,
            sourceType: Camera.PictureSourceType.CAMERA,
            allowEdit : false,
            encodingType: Camera.EncodingType.PNG,
            mediaType: Camera.MediaType.PICTURE,
            targetWidth: 250,
            targetHeight: 250,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: true
        };
 
    

        $cordovaCamera.getPicture(options).then(function(imageURI) {
          console.log('invokeing cordovaCamera');
          $scope.image =  imageURI;
         //  imageURI.substr(imageURI.lastIndexOf('/')+1);
          console.log(imageURI.substr(imageURI.lastIndexOf('/')+1));
          
           MyService.setImgPath(imageURI);
          console.log($scope.image);
          console.log(imageURI);
          $scope.upload();        
        }, function(err) {
            // An error occured. Show a message to the user
            console.log(err);
        });
        
    };

    $scope.upload =function(){

       var ping = MyService.getImgPath().substr(MyService.getImgPath().lastIndexOf('/')+1);
       console.log(ping);

      // Test code (for angularjs)
// Tip: Usually init/create $window.cordova.plugin.ftp will take some time, so set a `timeout()` to make sure it's ready.
//      But surely, the best and safest way is to listen `deviceready` event for cordova, or `$ionicPlatform.ready()` for ionic.
//      You can find more info in official docs of cordova or ionic.
$timeout(function() {
    if ($window.cordova.plugin.ftp) {
        $log.log("xtest: ftp: found");
        // 1. connect to one ftp server, then you can do any actions/cmds
        $window.cordova.plugin.ftp.connect("308.3d8.myftpupload.com", "wepopusers", "Chandru@123", function() {
            $log.log("xtest: ftp: connect ok");
            // 2. list one dir, note that just can be dir, not file
            $window.cordova.plugin.ftp.ls("/gopi", function(fileList) {
                $log.log("xtest: ftp: list ok");
                if (fileList && fileList.length > 0) {
                    $log.log("xtest: ftp: The last file'name is " + fileList[fileList.length - 1].name);
                    $log.log("xtest: ftp: The last file'type is " + fileList[fileList.length - 1].type);
                    $log.log("xtest: ftp: The last file'link is " + fileList[fileList.length - 1].link);
                    $log.log("xtest: ftp: The last file'size is " + fileList[fileList.length - 1].size);
                    $log.log("xtest: ftp: The last file'modifiedDate is " + fileList[fileList.length - 1].modifiedDate);
                   
                      // 4. upload local file to remote, you can rename at the same time. arg1: local file, arg2: remote file.
                      // make sure you can access and read the local file.
                      $window.cordova.plugin.ftp.upload("/" + ping, "/gopi/" + ping, function(percent) {
                          console.log(percent);
                            if (percent == 1) {
                                $log.log("xtest: ftp: upload finish");
                       
                               
                            } else {
                                $log.log("xtest: ftp: upload percent=" + percent*100 + "%");
                            }
                        }, function(error) {
                            $log.log("xtest: ftp: upload error=" + error);
                        });
                }
            }, function(error) {
                $log.log("xtest: ftp: list error: " + error);
            });
        });
    } else {
        $log.log("xtest: ftp: not found!");
    }
}, 2000);
    };

  });

In the following line,

$window.cordova.plugin.ftp.upload("/ping", "/gopi/ping", function(percent)

/ping refers to the image named 1467013143014.png.

However, I encountered the following error:

xtest: ftp: upload error=java.io.FileNotFoundException: /ping

For more information, please refer to the git-hub page cordova-ftp.

While I was able to successfully upload a file called default.prop to my FTP server, I am struggling to upload other files besides that specific one. How can I correctly access and upload other images to my FTP server?

I've double-checked the image path in the upload area but still couldn't upload the image successfully.

Any guidance on how to troubleshoot or resolve this issue would be greatly appreciated.

Answer №1

One thing that stands out to me is that the ping variable contains the file path, so using "/ping" will be interpreted as a string rather than a variable. You should try "/" + ping instead.

Additionally, the ping variable only holds the file path, not the file name. You can try using "/" + ping + FILE_NAME instead.

To avoid hardcoding the path, consider utilizing the cordova file plugin. Once you have installed the plugin, you can use

cordova.file.applicationDirectory
or
cordova.file.applicationStorageDirectory
to dynamically get the path without hardcoding it. More information on file paths can be found in the official file plugin documentation. I hope this information proves useful for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Version Control in Ionic React Projects

I am working on an Ionic React Project that was created using Ionic Cli ionic start with the blank template. How do I go about setting the project's version that appears in the package.json file? Is it enough to just update the version there, or sho ...

Having difficulty removing new or existing lines on StackBlitz

I attempted to experiment with React on StackBlitz, but I encountered a problem where I couldn't delete any lines of code. It seems that while I can add new lines of code, deleting them is not an option. Even when logging in with GitHub, the issue per ...

Is there a way to insert rows and columns into the table headers using React Material UI?

I am new to working with material UI and I am having trouble figuring out how to add columns in the table header. The image below shows what I am trying to achieve - under "Economics", there should be 3 columns, each of which will then have two more column ...

Achieve the capability to upload multiple files in Next.js using the upload.io integration feature

I'm currently using upload.io for uploads and replicate.com for an AI model on a specific app. I am able to upload one picture, but unfortunately, I am encountering issues when trying to upload multiple pictures. Can anyone identify the problem here? ...

Module Ionic not found

When I attempt to run the command "ionic info", an error is displayed: [ERROR] Error loading @ionic/react package.json: Error: Cannot find module '@ionic/react/package' Below is the output of my ionic info: C:\Users\MyPC>ionic i ...

MUI Modal overlay for stylish Ionic select dialogues

In my react application, I am utilizing the MUI library, but there are instances where I still need to incorporate Ionic framework in the code. ISSUE: When opening MUI modal windows that contain Ionic selects, the select modals end up being covered by the ...

Tips for loading a partial view page in a specific element using Angular JS after clicking a button

I'm facing an issue with my AngularJS partial view page not loading after a button click. I've set up the initial rendering page, but when we click the button, the partial view page doesn't render properly because the angular modules are not ...

Comparing PhoneGap and React.js to NativeScript for Android hybrid app development

At our organization, we experimented with creating a hybrid app development framework using Cordova/Phonegap as the foundation. Unfortunately, this approach resulted in significant performance issues. As a solution, we turned to react.js to enhance front ...

Transmitting MQTT information through an application programming interface

In my project using Ionic React, I am developing an application to showcase temperature data. To achieve this, I have established an API that transmits MQTT temperature information and utilize Axios for data retrieval. Despite my efforts, I am encountering ...

Jest tests are failing to render React IonDateTime component

While executing Jest on an Ionic React component, I encountered a test failure consistently, regardless of whether the component had a time value or not. test('IonDateTime display', () => { render(<IonDatetime data-testid="foo" ...