简体   繁体   English

离子使用$ http上传base64图像

[英]Ionic upload base64 image using $http

My server-side code already works using postman, it doesn't work using Ionic 我的服务器端代码已经可以使用邮递员使用,而不能使用Ionic

I'm trying to upload a image converted in base64 (about 2M) to a server using this function: 我正在尝试使用此功能将以base64(约2M)格式转换的图像上传到服务器:

this.postPic = function(image,tags,title){
    var dfd = $q.defer();
    var req = {
          method: 'POST',
          url: baseURL+"utility.uploadpic",
          data: "auth_token="+$localstorage.get("token")+"&tags="+tags+"&title="+title+"&base64_file="+image,
          headers:{
              'Content-Type' : 'application/x-www-form-urlencoded'
          }
    };
    $http(req).success(function(response){
      dfd.resolve(response);
    });
    return dfd.promise;
  }

It gives me an error during upload ( showing me the message "Error creating file" in the following code) 它在上传期间给我一个错误(在以下代码中显示“错误创建文件”消息)

function upload_pic($title,$tags,$base64_file){
        $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
        $desc = $title;
        $file_obj_string = base64_decode($base64_file);
        //Creating file from string
        $imgRaw = imagecreatefromstring( $file_obj_string);
        if ($imgRaw !== false) {
        .................
        }
        else{
            return "Error creating file";
        }

I don't know if it couldn't create the image from string because there is a size problem or something else. 我不知道是否无法从字符串创建图像,因为存在尺寸问题或其他问题。 Any idea? 任何想法?

Solved. 解决了。 I just had to replace all spaces (I do not know where they were created) with + 我只需要用+替换所有空格(我不知道它们是在哪里创建的)

$base64_file = str_replace(' ', '+', $base64_file);

I hope this is helpful for somebody :) 我希望这对某人有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM