Ionic - Upload file to FTP server

僤鯓⒐⒋嵵緔 提交于 2019-12-13 23:31:05

问题


I am working on an Ionic application and I need to upload a file to a FTP server. Please note that I am new to mobile dev in general and Ionic in particular. The target is iOs.

I found a lot of information regarding this cordova plugin but I am still unable to push my file to my FTP server. And I am wondering if it is even possible with Ionic...

Do you guys have a solution for this? Do you think it is possible?

Here is my code:

.controller("CsvCtrl", function($scope, $cordovaFileTransfer, $ionicLoading) { 
$scope.upload = function() {
  var options = {
    fileKey: "avatar",
    fileName: "test.png",
    chunkedMode: false,
    mimeType: "image/png",
    params: {
      value1: "<FTP_LOGIN>",
      value2: "<FTP_PASSWORD>"
    }
  };
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
    fs.root.getDirectory(
        "AppDirectory",
        {
            create: false
        },
        function(dirEntry) {
            dirEntry.getFile(
                "test.png", 
                {
                    create: false, 
                    exclusive: false
                }, 
                function gotFileEntry(fe) {
                    $ionicLoading.hide();
                    $cordovaFileTransfer.upload("ftp://MY_URL/", fe.toURL(), options).then(function(result) {
                      console.log("SUCCESS: " + JSON.stringify(result.response));
                    }, function(err) {
                      console.log("ERROR: " + JSON.stringify(err));
                    }, function (progress) {
                      // constant progress updates
                    });
                }, 
                function(error) {
                    $ionicLoading.hide();
                    console.log("Error getting file");
                }
            );
        }
    );
  },
  function() {
    $ionicLoading.hide();
    console.log("Error requesting filesystem");
  });
}
})

And here is the error I get:

2015-10-20 11:24:43.564 POC [1127:297648] FileTransferError {
  body = "";
  code = 3;
  "http_status" = 0;
  source = "file:///var/mobile/Containers/Data/Application/DE05616B-1FA2-47E2-972C-9A773480C2ED/Documents/AppDirectory/test.png";
  target = "ftp://MY_URL/";
}
2015-10-20 11:24:43.564 POC [1127:297648] File Transfer Error: You do not have permission to access the requested resource.
2015-10-20 11:24:43.571 POC [1127:297608] ERROR: {"code":3,"source":"file:///var/mobile/Containers/Data/Application/DE0561    6B-1FA2-47E2-972C-  9A773480C2ED/Documents/App/test.png","target":"ftp://MY_URL/","http_status":null,"body":null,"exception":null}

Thanks in advance for your help.

Regards


回答1:


If your server requires HTTP Basic Authentication, there are two options I can think of:

  1. Put the credentials in the url ftp://username:password@url
  2. directly set a custom header options.headers = {'Authorization': base64('username' + ':' + 'password') };

Note that base64 must be a function that encodes the credentials like this:

base64= function(credentials) {
    var hash = btoa(credentials);
    return "Basic " + hash;
};


来源:https://stackoverflow.com/questions/33233783/ionic-upload-file-to-ftp-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!