ds3464 2012-10-08 16:45
浏览 129

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

I have a problem in sending a base64 image string (embedded in json object) to the server.

here is the Js code:

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);
    }

}

and here is the 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);

here's $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";    
        }
    }

the problem is that once I visualize the image stored in the server (<img src=\"$img\" alt='' title='' class='box_img' name='img_view' /> where $img is the path of the image), I see something like this enter image description here

but my original image looks like this enter image description here

anyone can help me?

  • 写回答

1条回答 默认 最新

  • dongyang9813 2012-10-08 16:48
    关注

    Found the solution! just removed the line

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

    now the images look the same!!!!

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献