duanmou9228 2014-10-22 14:40
浏览 282

解码从JSON帖子收到的PHP中的Base64字符串

I'm working on a project where i can select an image (simple file select) and send it via JSON to a PHP MySQL insert page.

Upload page looks like this:

if (input.files && input.files[0]) {
                var FR = new FileReader();
                FR.onload = function(e) {
                    $('#img').attr("src", e.target.result);
                    var Naam = $('input[type=file]').val();
                    $('#base').text(e.target.result);
                    var Foto = e.target.result;
                    var Barcode = $('#barcode').val();
                    obj['Barcode'] = Barcode;
                    obj['Naam'] = Naam;
                    obj['Foto'] = Foto;

                    //execute ajax send

                    $.ajax({
                        url : 'insert.php',
                        type : 'POST',
                        data : obj,
                        dataType : 'json',
                        success : function(msg) {
                            alert("msg");
                        }
                    });

                    //$.post("insert.php", obj, function (data) {}, "json");
                    //alert("msg");
                };
                FR.readAsDataURL(input.files[0]);

and my PHP page:

$Barcode = $_POST["Barcode"];
$Naam = $_POST["Naam"];
$Name = preg_replace('/^.+[\\\\\\/]/', '', $Naam);
$Foto = base64_decode($_POST["Foto"]);

$query = "INSERT INTO voorraad_foto (barbody, location, foto) VALUES ('$Barcode','$Name','{$Foto}')";
$results = mysqli_query($db,$query);

And my table field is a BLOB.

But when it execute this, everything works fine except that it doesn't insert it as a blob, but pure string

I've tried with removing the

preg_replace('#data:image/[^;]+;base64,#', '', $Foto)

but doesn't make any difference, same when trying to add headers, but nothing..

What am i doing wrong, or is there something obvious that i'm not getting?

Thx.

  • 写回答

1条回答 默认 最新

  • dongyuluan7494 2014-10-24 07:34
    关注

    I solved it in some kind of way:

    I wrote a function that gets the Base64 string, decodes it and writes it to a Temp file. Then i read that file again and upload that to my databse. When success, delete the file.

    It may not be the most effecient way, but it works!

    function WriteToImageAndGetData($base64_string, $File) {
    //Write to file
    $ifp = fopen($File, "wb");
    $data = explode(',', $base64_string); //Split Base64 string
    fwrite($ifp, base64_decode($data[1])); //Decode Base64 string
    fclose($ifp);
    
    //Read from file
    $fp = fopen($File, 'r');
    $data = fread($fp, filesize($File)); //Read file contents
    $data = addslashes($data);
    fclose($fp);
    return $data;
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思