我正在处理Ionic应用程序,我需要将文件上传到FTP服务器。 请注意,我是移动设备的新手,尤其是Ionic。 目标是iOs。
我发现了很多关于this cordova plugin的信息,但我仍然无法将文件推送到我的FTP服务器。而且我想知道Ionic是否有可能......
你们有解决方案吗?你觉得有可能吗?
这是我的代码:
.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");
});
}
})
这是我得到的错误:
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}
提前感谢您的帮助。
此致
答案 0 :(得分:1)
如果您的服务器需要HTTP基本身份验证,我可以考虑两个选项:
options.headers = {'Authorization': base64('username' + ':' + 'password') };
base64= function(credentials) {
var hash = btoa(credentials);
return "Basic " + hash;
};
请注意,base64必须是一个对凭据进行编码的函数,如下所示:
{{1}}