js:将base64字符串发送到php服务器页面

时间:2012-10-08 16:45:20

标签: php javascript json file-upload base64

我在向服务器发送base64图像字符串(嵌入在json对象中)时遇到问题。

这是Js代码:

var sMimeType;
var id_g;
var tipo_g;
var lang_g;

function fileLoaded(frEvnt){
    var sFBody = frEvnt.target.result;
    var sBodyBase64 = btoa(sFBody);
    var myobj = {my_id: id_g, my_tipo: tipo_g, my_lang:lang_g, img_mime_type: sMimeType, img_base64 :sBodyBase64};
    //$("#error").html(JSON.stringify(myobj));
    $.ajax({
      url: "up_img.php",
      data: JSON.stringify(myobj),
      type: 'POST',
      success:function(a) {/*window.location = tipo_g+".php?l="+lang_g*/alert(a);},
      error:function() {$("#error").html("error")}
    });

}
function Upload(id,tipo){
    lang = document[id].l.value;
    var oFile = document[id].img.files[0];
    id_g = id;
    tipo_g = tipo;
    lang_g = lang;
    if(oFile){
        var oFReader = new FileReader();
        oFReader.onload = fileLoaded;
        sMimeType = oFile.type;
          oFReader.readAsBinaryString(oFile);
    }

}

这是up_img.php

require_once('dbconn.php');
require_once('../utility/sec.php');


$json = json_decode(file_get_contents("php://input"),true);

$id = "";
$id = $json['my_id'];
$lang = "";
$lang = $json['my_lang'];
$tipo = "";
$tipo = $json['my_tipo'];
$img_mime = "";
$img_mime = $json['img_mime_type'];
$img_base64 = "";
$img_base64 = base64_decode($json['img_base64']);
$event = new event($tipo,$lang);


$event->upload_img($img_base64,$img_mime,$id);

这里是$event->upload_img($img_base64,$img_mime,$id);

public function upload_img($img,$mime,$id){
        if($this->check_img($id)){
            //$img = str_replace('data:image/png;base64,', '', $img);
            switch($mime){
                case "image/png": case "image/jpeg": case "image/jpg": case "image/gif": case "image/tiff":{
                    $img = str_replace(' ', '+', $img);
                    $data = $img;
                    $ext = explode('/',$mime);
                    $file = $this->get_folder_picture() . md5($id) . '.' . $ext[1];
                    $success = file_put_contents($file, $data);
                    if($success)
                    {
                         $newconn = new event($this->getTipo(),$this->getLang());
                         $newconn->open_conn();
                         $query = sprintf("insert into images (`Id`,`Ext`) values (%d,'%s')",
                            $id,$ext[1]);
                         $q = mysql_query($query,$newconn->getLink());
                         echo mysql_error($newconn->getLink());
                         $newconn->close_conn();

                    }else
                        echo 'Unable to save the file.';
                    break;
                }
                default:{
                    echo "not a valid image. image type accepted are jpge,jpg,png,tiff and gif: u gave ".$mime; 
                    break;
                }

            }

        }else{
            print "image error";    
        }
    }

问题是,一旦我想象存储在服务器中的图像(<img src=\"$img\" alt='' title='' class='box_img' name='img_view' /> $img是图像的路径),我看到这样的东西 enter image description here

但我的原始图片看起来像这样 enter image description here

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:4)

找到解决方案!刚刚删除了这行

$img = str_replace(' ', '+', $img);

现在图像看起来一样!!!!