繁体   English   中英

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

[英]application/x-www-form-urlencoded between ionic and php

我正在创建一个Ionic应用程序,并且正在使用PHP服务器,以便将使用移动设备拍摄的照片上传到服务器。 我正在使用的PHP API已经在产品中开发,并且在我的Web版本上没有任何问题,但是当我尝试使用相同的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文件中的文本完全相同。

您能否让我知道标题/函数中是否存在任何错误,还是应该尝试使用其他方式上传文件?

先感谢您

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

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types

最后,我决定更改我的API,以便使用其他选项,例如由ionic提供的uploadfile。

暂无
暂无

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

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