首頁  >  文章  >  後端開發  >  javascript - AJax post BASE64編碼到後台

javascript - AJax post BASE64編碼到後台

WBOY
WBOY原創
2016-08-25 10:37:281321瀏覽

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輸出 的圖片都無法顯示正確

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn