【问题标题】:ionic app image upload from camera photo library but the filename img ".Pic.jpg"从相机照片库上传离子应用图像,但文件名 img “.Pic.jpg”
【发布时间】:2016-10-15 20:27:08
【问题描述】:

我使用this code 在我的 ionic 应用程序中上传图像。一切顺利,除了文件名。

文件名变成“.Pic.jpg”

为什么会发生这种情况,我应该怎么做才能克服这个问题? 具体来说,如何使上传图像的文件名与所选图像的名称相同?

【问题讨论】:

    标签: android ionic-framework file-transfer ngcordova


    【解决方案1】:

    我检查了你被推荐的代码。在那个上传的文件中,但是文件名丢失了。为此,您需要提及要上传文件的图像名称。例如

           var options = {
              quality: 90,
              destinationType: Camera.DestinationType.FILE_URI,
              sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
              allowEdit: true,
              encodingType: Camera.EncodingType.JPEG,
              popoverOptions: CameraPopoverOptions,
              saveToPhotoAlbum: false
            };
    
            $cordovaCamera.getPicture(options).then(function(imageData) {  
              $scope.profileImageData = imageData;  
    
              $scope.profileImageName = Math.floor(Math.random()*16777215).toString(16)
    
              var server = baseUrlImageUpload+'upload.php';
              filePath = imageData;
              var date = new Date();
    
              var options = {
                fileKey: "file",
                fileName: $scope.profileImageName,
                chunkedMode: false,
                mimeType: "image/jpg"
              };
    
              $cordovaFileTransfer.upload(server, filePath, options).then(function(result) {
                $ionicLoading.hide();
                //alert(JSON.stringify(result.response));
    
              }, function(err) {
               $ionicLoading.hide(); 
               //alert("Select image again") 
    
                var alertPopup = $ionicPopup.alert({
                  title: 'Sorry!',
                  template: 'Please select image again.'
                });
    
              }, function (progress) {
                 $ionicLoading.show({
                   template: 'Updating Image...'
                }); 
              });
    
            });
    

    在上面的代码中,我随机传递图像名称 $scope.profileImageName = Math.floor(Math.random()*16777215).toString(16)。

    你的php代码是这样的

    <?php
    
      if ($_FILES["file"]["error"] > 0) {
        echo "Error Code: " . $_FILES["file"]["error"] . "<br />";
      }  else  {
    
      if ($_FILES["file"]["size"] < 1024 * 1024) {
        if (file_exists("/files/".$_FILES["file"]["name"]))
        {
          echo $_FILES["file"]["name"] . " already exists. No joke-- this error is almost <i><b>impossible</b></i> to get. Try again, I bet 1 million dollars it won't ever happen again.";
        }  else  {
          $number = rand(111111,999999);
        move_uploaded_file($_FILES["file"]["tmp_name"], 'images/' .$_FILES["file"]["name"].'.jpg');
        echo "Done";
        //return $number;
        }
      }  else {
         // echo "File Size: " . ($_FILES["file"]["size"] / 1024 * 1024) . " MB<br />";
        echo "Please upload below 1MB images";
      }
    }
    
    ?>
    

    希望对你有帮助:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      相关资源
      最近更新 更多