Javascript-ajax post BASE64 encode to background

Source: Internet
Author: User
Tags base64 encode
php convert & output BASE64

$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;

JSON format returned not processed directly data.results obtained cannot be displayed

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv/bAEMABgQFBgUEBgYFBgcHBggKEAoKCQkKFA4PDBAXFBgYFxQWFhodJR8aGyMcFhYgLCAjJicpKikZHy0wLSgwJSgpKP/bAEMBBwcHCggKEwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKP/A....

It's the one that's in sync and you put it on the html label. li data-base64

The following is an asynchronous commit, Mr. Cheng argument

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(','),});

Background receive processing

php

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

Then directly insert the database field type Yes longtext ,

Each step of the JSON output cannot be displayed correctly.

Reply content:

php convert & output BASE64

$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;

JSON format returned not processed directly data.results obtained cannot be displayed

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv/bAEMABgQFBgUEBgYFBgcHBggKEAoKCQkKFA4PDBAXFBgYFxQWFhodJR8aGyMcFhYgLCAjJicpKikZHy0wLSgwJSgpKP/bAEMBBwcHCggKEwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKP/A....

It's the one that's in sync and you put it on the html label. li data-base64

The following is an asynchronous commit, Mr. Cheng argument

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(','),});

Background receive processing

php

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

Then directly insert the database field type Yes longtext ,

Each step of the JSON output cannot be displayed correctly.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.