使用cordova filetransfer通过https上传文件 [英] Uploading file over https with cordova filetransfer

查看:449
本文介绍了使用cordova filetransfer通过https上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件传输插件的问题,我似乎无法搞清楚。当我使用http时,我的代码可以工作,但是当我尝试通过https上传时,似乎它不会将参数发送到我的api,但它确实达到了我的api。只有文件丢失,x-session-token标头存在且有效。这是我用于上传文件的代码:

  $ scope.options = {}; 
$ scope.options.fileKey =file;
$ scope.options.fileName = $ scope.img.substr($ scope.img.lastIndexOf('/')+ 1);
$ scope.options.mimeType =image / jpeg;
$ scope.options.headers = {
'x-session-token':window.localStorage.auth
};

//参数:source,filePath,options
$ cordovaFile.uploadFile(https:/ **** / api.php / profielpic /,$ scope.img,$ scope .options,true).then(function(result){
console.log(result);
$ cordovaToast.showShortTop('Uploaden gelukt!')。then(function(success){
$ ionicLoading.hide();
$ state.go('tab.profiel');
},函数(错误){
$ ionicLoading.hide();
$ state.go('tab.profiel');
});

},函数(错误){
console.log(错误);
$ ionicLoading.hide();
});

这是我使用服务器端查看是否有任何内容的代码:

  $ app-> post('/ profielpic /',function()use($ app){

$ auth = new api\src\Auth;
$ users = new api\src\Users;
$ authdata = json_decode($ app-> request-> headers-> get(' x-session-token'));
$ data = json_decode($ app-> request-> getBody());
$ userid = $ authdata-> userID;
$ session_token = $ authdata-> session_token;
$ userdata = $ data-> userdata;
$ alertsarray = array();
$ message = null;
$ isValid = true;
$ authresult = $ auth-> authenticate($ userid,$ session_token);

$ imgname = time();
print_r(json_encode($ authdata) );
print_r(json_encode($ _ FILES));
print_r(json_encode($ _ POST));
print_r(json_encode($ data));
print_r(json_encode(file_get_contents) (php:// input)));
/ *
if($ authresult === true){
$ res = $ users-> updateUserPicture($ userid,$ _FILES ['file']);
if($ res === false){
$ isValid = false;
$ message =Er ging iets mis。;
} else {
$ message = $ res;
}
}其他{
$ isValid = true;
$ message = $ authresult;
} * /

$ dataArray = array(
'isValid'=> $ isValid,
'message'=> $ message
) ;

echo)]}',\ n.json_encode($ dataArray);

});



但是所有内容都是空的https://如果我上传到http://它有效



任何人都知道为什么http工作,但https不起作用? https不起作用的唯一情况是文件上传。我的api路线的其余部分都可以使用https。



它发生在iOS设备和Android设备上,所以问题更可能出现在我认为的微弱api上/ p>

Api回复:

  {
bytesSent: 32889,
responseCode:200,
response:{\userID \:\2 \,\session_token \:\* ***
} {\
file \:{\name \:\modified.jpg?1427448587960 \,\type \ :\ image\\ / jpeg\,\ tmp_name\:\ \\ / tmp\\ / phpABKyF2\,\ error\: 0,\ size\:37491}} [] null\ \)]},\\\
{\ isValid\:真,\ message\:空},
objectId:
}


解决方案

确保使用的是有效的SSL证书。如果您的应用已达到API但未发送数据,则可能是您的应用已意识到连接不安全。它只会在建立与其可信任的服务器的安全连接后才会发送数据。



自签名证书值得信任。


I've got a problem with the file transfer plugin that i can't seem to figure out. My code works when i'm using http, but when I try to upload over https it seems that it doesn't send the parameters to my api but it does reach my api. only the the file is missing, the x-session-token header is present and valid. This is the code i use for uploading the file:

$scope.options = {};
$scope.options.fileKey = "file";
$scope.options.fileName = $scope.img.substr($scope.img.lastIndexOf('/') + 1);
$scope.options.mimeType = "image/jpeg";
$scope.options.headers = {
    'x-session-token' : window.localStorage.auth
}; 

// parameters: source, filePath, options
$cordovaFile.uploadFile("https:/****/api.php/profielpic/", $scope.img, $scope.options, true).then(function(result) {
  console.log(result);
  $cordovaToast.showShortTop('Uploaden gelukt!').then(function(success) {
    $ionicLoading.hide();
    $state.go('tab.profiel');
  }, function (error) {
    $ionicLoading.hide();
    $state.go('tab.profiel');
  });

}, function(err) {
  console.log(err);
  $ionicLoading.hide();
});

This is the code i use Server side to see if there's anything:

$app->post('/profielpic/', function () use ($app) {

$auth           = new api\src\Auth;
$users          = new api\src\Users;
$authdata       = json_decode($app->request->headers->get('x-session-token'));
$data           = json_decode($app->request->getBody());
$userid         = $authdata->userID ;
$session_token  = $authdata->session_token;
$userdata       = $data->userdata;
$alertsarray    = array();
$message        = null;
$isValid        = true;
$authresult     = $auth->authenticate($userid, $session_token);

$imgname = time();
print_r(json_encode($authdata));
print_r(json_encode($_FILES));
print_r(json_encode($_POST));
print_r(json_encode($data));
print_r(json_encode(file_get_contents("php://input")));
/*
if($authresult === true) {
    $res = $users->updateUserPicture($userid, $_FILES['file']);
    if($res === false) {
        $isValid = false;
        $message = "Er ging iets mis.";
    }else{
        $message = $res;
    }
}else { 
    $isValid = true;
    $message = $authresult; 
}*/

$dataArray = array(
    'isValid' => $isValid,
    'message' => $message
);

echo ")]}',\n".json_encode($dataArray);

});

but everything is empty with https:// if i upload to http:// it works

Does anyone know why http works but https isn't working? the only case where https isn't working is with file uploads. the rest of my api routes work with https.

It happens on iOS devices and Android devices so the problem is more likely to be with the slim api i'd guess

Api response:

{
    "bytesSent": 32889,
    "responseCode": 200,
    "response": "{\"userID\":\"2\",\"session_token\":\"****"
} {\
    "file\":{\"name\":\"modified.jpg?1427448587960\",\"type\":\"image\\/jpeg\",\"tmp_name\":\"\\/tmp\\/phpABKyF2\",\"error\":0,\"size\":37491}}[]null\"\")]}',\n{\"isValid\":true,\"message\":null}",
    "objectId": ""
}

解决方案

Make sure you are using a valid SSL certificate. If your app is reaching the API but not sending the data, its likely that your app has realised the connection is unsafe. It will only send data once it establishes a secure connection to a server it can trust.

A self signed certificate will not be trusted.

这篇关于使用cordova filetransfer通过https上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆