Uploading file over https with cordova filetransfe

2020-07-27 23:40发布

问题:

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": ""
}

回答1:

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.