Ionic
本文介绍了Ionic - 将文件上传到FTP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述我正在使用Ionic应用程序,我需要将文件上传到FTP服务器。 请注意,我是移动开发的新手,尤其是Ionic。 目标是iOs。
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.
我发现了很多关于这个cordova插件,但我仍然无法将我的文件推送到我的FTP服务器。我想知道如果它是可能的离子...
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?
这是我的代码:
.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"); });}})这里是我得到的错误: p>
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}先感谢您的帮助。
Regards
推荐答案如果您的服务器需要HTTP基本身份验证,我可以想到两个选项:
If your server requires HTTP Basic Authentication, there are two options I can think of:
将凭证放在网址 ftp:// username:password @ url 直接设置自定义标题 options.headers = {'Authorization':base64('username'+':'+'password')}; li> Put the credentials in the url ftp://username:password@urldirectly set a custom header options.headers = {'Authorization': base64('username' + ':' + 'password') };请注意,base64必须是一个编码如下所示凭证的函数:
Note that base64 must be a function that encodes the credentials like this:
base64= function(credentials) { var hash = btoa(credentials); return "Basic " + hash;};Ionic