Heim  >  Artikel  >  Backend-Entwicklung  >  javascript - AJax post BASE64编码到后台

javascript - AJax post BASE64编码到后台

WBOY
WBOYOriginal
2016-08-25 10:37:281321Durchsuche

php convert & output BASE64

<code class="php">$img = str_replace(FILE_DOMAIN , FILE_PATH, 'http://img.demo.com/2016/08/21/12121.jpg');
if(! file_exists($img)) {
    $ret = array('err_no'=>-1,'err_msg' => '商品图片不存在');
    break;
}

$img_info = getimagesize($img);
$img_base64 = "data:{$img_info['mime']};base64,".chunk_split(base64_encode(file_get_contents($img)));
$result['base64'] = $img_base64;

$ret = array('err_no' => 0, 'err_msg'=> 'succes', 'results'=> $result);

echo json_encode($ret, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE  ); exit;</code>

json格式返回 没处理直接 data.results 获取的 无法显示

<code class="text">data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv/bAEMABgQFBgUEBgYFBgcHBggKEAoKCQkKFA4PDBAXFBgYFxQWFhodJR8aGyMcFhYgLCAjJicpKikZHy0wLSgwJSgpKP/bAEMBBwcHCggKEwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKP/A....</code>

上面是同步的 说以 放到 html 标签的lidata-base64 你面

下面是异步提交,先生成参数

js

<code class="js">var idArr = [];
var imgArr =[];
$(".list-group").children('li').each(function(index){
    var imgBase64 = $(this).data('base64');
    var pId = $(this).data('item');
    idArr.push(pId); imgArr.push( encodeURIComponent(imgBase64));
});

//生成参数
var _params = $.param({
    pid: idArr.join(','),
    image: imgArr.join(','),
});</code>

后台接收处理

php

<code class="php">$img_arr = explode(',', $params['image']);
foreach($result as $key => $val) {
    $ans_data[] = array(
        'image'   => addslashes( rawurldecode( $img_arr[$key]) ),
    );
}
                    </code>

然后就直接插入数据库 字段类型是longtext

这里每一步json输出 的图片都无法显示正确

回复内容:

php convert & output BASE64

<code class="php">$img = str_replace(FILE_DOMAIN , FILE_PATH, 'http://img.demo.com/2016/08/21/12121.jpg');
if(! file_exists($img)) {
    $ret = array('err_no'=>-1,'err_msg' => '商品图片不存在');
    break;
}

$img_info = getimagesize($img);
$img_base64 = "data:{$img_info['mime']};base64,".chunk_split(base64_encode(file_get_contents($img)));
$result['base64'] = $img_base64;

$ret = array('err_no' => 0, 'err_msg'=> 'succes', 'results'=> $result);

echo json_encode($ret, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE  ); exit;</code>

json格式返回 没处理直接 data.results 获取的 无法显示

<code class="text">data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv/bAEMABgQFBgUEBgYFBgcHBggKEAoKCQkKFA4PDBAXFBgYFxQWFhodJR8aGyMcFhYgLCAjJicpKikZHy0wLSgwJSgpKP/bAEMBBwcHCggKEwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKP/A....</code>

上面是同步的 说以 放到 html 标签的lidata-base64 你面

下面是异步提交,先生成参数

js

<code class="js">var idArr = [];
var imgArr =[];
$(".list-group").children('li').each(function(index){
    var imgBase64 = $(this).data('base64');
    var pId = $(this).data('item');
    idArr.push(pId); imgArr.push( encodeURIComponent(imgBase64));
});

//生成参数
var _params = $.param({
    pid: idArr.join(','),
    image: imgArr.join(','),
});</code>

后台接收处理

php

<code class="php">$img_arr = explode(',', $params['image']);
foreach($result as $key => $val) {
    $ans_data[] = array(
        'image'   => addslashes( rawurldecode( $img_arr[$key]) ),
    );
}
                    </code>

然后就直接插入数据库 字段类型是longtext

这里每一步json输出 的图片都无法显示正确

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn