dqc19941228 2018-06-16 17:23
浏览 105
已采纳

application / x-www-form-urlencoded在ionic和php之间

I'm creating an Ionic app and I'm using a PHP server in order to upload the pictures taken with my mobile device to my server. The PHP API that I'm using it's already developed in prod and working without any issues on my web version but I'm having problems when I try to use the same POST request.

My Ionic code:

upload_image():Observable<any>{
    //This is not the complete string.
    let _image64 = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABALDA4MChAODQ4SERATGC";
    let params = `image64_str=${_image64}`;
    let headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
    let httpOptions = {headers};
    return this.http.post<any>("http://API_URL",params,httpOptions);
}

My PHP code:

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header('Content-type: application/x-www-form-urlencoded');
$strimg = $_POST['image_str64'];
$image_str = substr($strimg ,strpos($strimg , ',')+1); //To avoid "data:image/jpeg;base64," from the 64string
$data = base64_decode($image_str);
//
//rest of code to check the format, directory and so one.
//

The problem is that when I'm calling this API from my Ionic function, I'm getting the following error:

<b>Warning</b>:  imagecreatefromstring(): gd-jpeg: JPEG library reports unrecoverable error:  in <b>/API/test.php</b> on line <b>57</b>
<b>Warning</b>:  imagecreatefromstring(): Passed data is not in 'JPEG' format in <b>/API/test.php</b> on line <b>57</b>
<b>Warning</b>:  imagecreatefromstring(): Couldn't create GD Image Stream out of Data in <b>/API/test.php</b> on line <b>57</b>

The "interesting point" is that if I "hardcode" the same string base64Data taken from Ionic app into my PHP file, the upload works without any problem. Also I have checked that the string send from my Ionic app is exactly the same text that I receive into my PHP file throught $_POST['varialbe'] option.

Can you please let me know if there is any eror within my headers/functions or if I should try to use another way to upload my file?

Thank you in advance

  • 写回答

2条回答 默认 最新

  • duanliao5995 2018-06-26 08:05
    关注

    Finally I have decided to change my API by in order to use others options like uploadfile provided by ionic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名