duanpenpan5796 2014-05-12 19:20
浏览 60

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

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.

The issue i am having is, getting the full base64 code to the php file. Here is my jquery code:

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:

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.

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. Based on the console, its not seeing all of the base64.

UPDATE

By removing the json.stringify as per the first answer, the raw base64 was not formatted correctly, with the wrong chars in it. 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. I see it in console, check it against a img decoder, works fine, but the $_POST is empty ?

UPDATE 2

I have changed it from $_POST to this:

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. 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 :(

  • 写回答

1条回答 默认 最新

  • dthp96899 2014-05-12 19:24
    关注

    You're telling jquery to send JSON, but then manually encoding your object. That means the data parameter, in the ajax function, as written, will just be a plain string. Since you've told jquery to send json, it'll RE-encode that string.

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值