简体   繁体   English

通过ajax从手机到PHP的base64无法正常工作

[英]base64 from mobile to php via ajax not working

I am trying, without any success, even with researching, to have a photo taken on a mobile device, which puts into a base64 long string, and send it to a php that then uploads it a database. 我正在尝试即使没有进行研究也没有取得成功,但是在移动设备上拍摄了一张照片,将其放入base64长字符串中,并将其发送到php,然后将其上传到数据库中。

The issue i am having is, getting the full base64 code to the php file. 我遇到的问题是,将完整的base64代码获取到php文件中。 Here is my jquery code: 这是我的jQuery代码:

var imgData = localStorage.getItem('imgFile');
var propid = 213;
var tbl = 'tbl_images';
var sitch = 'images';

//console.log(imgData);

$.ajax({
   url: 'http://website.com/info/fn.php',
   dataType: 'json',
   data: JSON.stringify({'switch' : sitch, table : tbl, propid : propid, imgData : imgData}),
   type: 'POST',


    success: function(data) {
        console.log(data);

    }

}); });

This is how the json looks: 这是json的样子:

data%3Aimage%2Fjpeg%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAAPoAAAD6CAYAAACI7Fo9AAAAHGlET1QAAAACAAAAAAAAAH0AAAAoAAAAfQAAAH0AASpQSGstYwAAQABJREFUeAFsnXV7Xee17f0tbpnTpJQ0aCcOmCVZLFkWmpmZmSXbAoPMtmyZZBIzyxxmhiZpSmlTOs1pe9Jm3N94l3aa89z7x3zWXmuvvST58XjHmGPOd%2B4Bf5X0X18Kn%2F%2BF%2BDPxx%2F74m

So going to the PHP its looking like, and thus coming back, so even if it was the full length, the '%' will put it off from working. 因此,转到PHP的外观,然后返回,因此即使它是完整长度,“%”也将使它无法正常工作。

So I would like some help on how to ensure the full code is going, and ensure its in the correct format. 因此,我希望获得有关如何确保完整代码进行并确保其格式正确的帮助。

Thanks in advance. 提前致谢。

NOTE: I did not put the PHP file here, as I did not think thats the problem, it is receiving the data, but just not all of it. 注意:我没有将PHP文件放在这里,因为我不认为这是问题所在,它正在接收数据,但不是全部。 Based on the console, its not seeing all of the base64. 基于控制台,它看不到所有的base64。

UPDATE UPDATE

By removing the json.stringify as per the first answer, the raw base64 was not formatted correctly, with the wrong chars in it. 通过按照第一个答案删除json.stringify,原始base64的格式不正确,其中包含错误的字符。 If I leave it in. It does format right, and i get the full raw image data. 如果我将其保留。它确实可以正确格式化,并且我可以获得完整的原始图像数据。 But then my PHP file is not receiving it. 但是然后我的PHP文件没有收到它。 I see it in console, check it against a img decoder, works fine, but the $_POST is empty ? 我在控制台中看到它,对照img解码器检查它,工作正常,但$ _POST为空?

UPDATE 2 更新2

I have changed it from $_POST to this: 我已将其从$_POST更改为:

if(!$_POST) {
    $data = json_decode(file_get_contents('php://input'));
    print_r($data);
    $switch = $data->switch;
} else {
    $switch = $_POST['switch'];
}

I can see using console.log, that the full image is sent in the POST via Ajax using the code above. 我可以使用console.log看到完整的图像是使用上面的代码通过Ajax在POST中发送的。 When i think put this into a query string, it does not take the whole binary, just a small part? 当我认为将其放入查询字符串时,它不会占用整个二进制文件,只是一小部分?

I cannot work out why it's not playing :( 我不知道为什么它不播放:(

You're telling jquery to send JSON, but then manually encoding your object. 您是在告诉jquery发送JSON,然后手动编码您的对象。 That means the data parameter, in the ajax function, as written, will just be a plain string. 这意味着,按照编写的方式,ajax函数中的data参数将只是一个纯字符串。 Since you've told jquery to send json, it'll RE-encode that string. 既然您已经告诉jquery发送json,它将重新编码该字符串。

You should have only 你应该只有

data: {'switch' : sitch, table : tbl, propid : propid, imgData : imgData}

jquery will do the grunt work of converting that object to json for you. jQuery将为您完成将对象转换为json的艰巨工作。

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

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