离子cordovaFileTransfer与api php代码错误3

时间:2017-05-03 09:50:58

标签: php cordova ionic-framework upload

我正在开发一个共享照片的应用程序,我使用ionic作为移动部件,php使用后端部分。但是当我上传超过2M的文件时,我遇到了问题。 非常感谢您的美好时光。 这是我的代码: // PHP后端:upload.php

 <?php
    header('Access-Control-Allow-Origin: *');
    $target_path = "uploads/";
    $target_path = $target_path . basename( $_FILES['file']['name']);
    if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
    echo "Upload and move success";
    } else{
    echo $target_path;
        echo "There was an error uploading the file, please try again!";
    }
    header ("Connection: close");
    ?>

// Ionic js

    $scope.selectPicture = function(sourceType) {
     var options = {
    quality: 100,
    destinationType: Camera.DestinationType.FILE_URI,
    sourceType: sourceType,
    saveToPhotoAlbum: false//,
    //targetWidth: 800, 
    //targetHeight: 800
     };        


     $cordovaCamera.getPicture(options).then(function(imagePath) {
    // Grab the file name of the photo in the temporary directory
    var currentName = imagePath.replace(/^.*[\\\/]/, '');

    //Create a new name for the photo
    var d = new Date(),
    n = d.getTime(),
    newFileName =  n + ".jpg";

    // If you are trying to load image from the gallery on Android we need special treatment!
    if ($cordovaDevice.getPlatform() == 'Android' && sourceType === Camera.PictureSourceType.PHOTOLIBRARY) {
      window.FilePath.resolveNativePath(imagePath, function(entry) {
        window.resolveLocalFileSystemURL(entry, success, fail);
        function fail(e) {
          console.error('Error: ', e);
        }

        function success(fileEntry) {
          var namePath = fileEntry.nativeURL.substr(0, fileEntry.nativeURL.lastIndexOf('/') + 1);
          // Only copy because of access rights
          $cordovaFile.copyFile(namePath, fileEntry.name, cordova.file.dataDirectory, newFileName).then(function(success){
            $scope.image = newFileName;
          }, function(error){
            $scope.showAlert('Error', error.exception);
          });
        };
      }
    );
    } else {
      var namePath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
      // Move the file to permanent storage
      $cordovaFile.moveFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(function(success){
        $scope.image = newFileName;
      }, function(error){
        $scope.showAlert('Error', error.exception);
      });
    }
  },
  function(err){
    // Not always an error, maybe cancel was pressed...
  })
};
// Returns the local path inside the app for an image
$scope.pathForImage = function(image) {
  if (image === null) {
    return '';
  } else {
    return cordova.file.dataDirectory + image;
  }
};
$scope.uploadImage = function() {
  // Destination URL
  var url = "http://myssite.com/upload.php";

  // File for Upload
  var targetPath = $scope.pathForImage($scope.image);

  // File name only
  var filename = $scope.image;;

  var options = {
    fileKey: "file",
    fileName: filename,
    chunkedMode: false,
    mimeType: "multipart/form-data",
    params : {'fileName': filename},
    headers : { Connection: "close"}
  };
  $cordovaFileTransfer.upload(url, targetPath, options).then(function(result) {
      console.log("SUCCESS: " + JSON.stringify(result.response));
  }, function (err) {
      console.log("ERROR: " + JSON.stringify(err));
  }, function (progress) {
    $timeout(function () {
      $scope.downloadProgress = (progress.loaded / progress.total) * 100;
      console.log("progress: " + progress + " dddd "+JSON.stringify(progress));
      console.log("mytime "+$scope.downloadProgress);
      $scope.progressval = $scope.downloadProgress;
    });
  });

我的配置:上传最大文件大小:40M             最大尺寸:40M

这是我的错误:错误:{“code”:3,“source”:“file:///data/user/0/com.ionicframework.s ample374926 / files / 14 93805852934.jpg”, “ta rget”:“xxxxxx / ...连接时的流结束{xxxx.:80,proxy = DIRECT @ hostAddress = xxxxxx cipherSuite = none protocol = http / 1.1}(recycle count = 0)”}

0 个答案:

没有答案