ajax 传输base64图片给php保存

首先:将base64码里的“+”号改成"%2B"和“&”改成"%26" 因为在ajax传送时会把他们变成空格 毁坏了数据的正确性

str=str.replace(/\&/g,"%26");

str=str.replace(/\+/g,"%2B");

其次:ajax要用post传送,GET无法传送那么多的数据量!

最后:

$filename=date("dMYHis").".png";//要生成的图片名字

function convert_data($data){

$image = base64_decode( str_replace('data:image/jpeg;base64,', '',$data); //记得要将base64解码,还有去除base64码前缀

header('Content-Type: image/png');

save_to_file($image);

}

function save_to_file($image){

$fp = fopen($filename, 'w');

fwrite($fp, $image);

fclose($fp);

}