【问题标题】:application/x-www-form-urlencoded between ionic and phpionic 和 php 之间的 application/x-www-form-urlencoded
【发布时间】:2018-11-26 03:44:47
【问题描述】:

我正在创建一个 Ionic 应用程序,并且我正在使用 PHP 服务器来将使用我的移动设备拍摄的照片上传到我的服务器。 我使用的 PHP API 已经在 prod 中开发,并且在我的网络版本上没有任何问题,但是当我尝试使用相同的 POST 请求时遇到问题。

我的离子密码:

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);
}

我的 PHP 代码:

<?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.
//

问题是当我从 Ionic 函数调用此 API 时,我收到以下错误:

<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>

“有趣的一点”是,如果我将从 Ionic 应用程序获取的相同字符串 base64Data “硬编码”到我的 PHP 文件中,则上传工作没有任何问题。 我还检查了从我的 Ionic 应用程序发送的字符串与我通过 $_POST['varialbe'] 选项接收到我的 PHP 文件中的文本完全相同。

如果我的标题/函数中有任何错误,或者我是否应该尝试使用其他方式上传我的文件,请告诉我?

提前谢谢你

【问题讨论】:

    标签: javascript php angular ionic-framework


    【解决方案1】:

    在您的 Ionic + PHP 应用程序中尝试将 Content-Type 设置为 image/jpeg

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

    【讨论】:

    • 我会尝试,但我想我不得不更改我的 API 文档,我不想这样做。
    【解决方案2】:

    最后我决定更改我的 API 以使用其他选项,例如 ionic 提供的上传文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-11
      • 2019-02-04
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 2015-06-18
      相关资源
      最近更新 更多