dqc19941228
2018-06-16 17:23
浏览 102

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.

    点赞 打赏 评论
  • duanhao1004 2018-06-16 20:06

    In your Ionic + PHP app try setting the Content-Type to image/jpeg

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types

    点赞 打赏 评论

相关推荐 更多相似问题