前端对input file 进行base64进行加密

<form action="javascript:;"  method="post" id="commentform" enctype="application/x-www-form-urlencoded">注意这个属性才能发送文件

<input type="file" name="uploadA" id="uploadA" value=""accept="image/*" />

</form>

$("#uploadA").change(function(e){
console.info(e.target.files[0]);//图片文件
var dom =$("input[id^='uploadA']")[0];
var reader = new FileReader();
reader.onload = (function (file) {
return function (e) {
var html='<div class="show_img" style="height: 80px;width: 80px;margin-right: 10px;position:relative;">';
html+='<div class="closeShowImg" style="height: 16px;width:16px;background: #B8956E;position:absolute;right:-5px;top:-5px;border-radius:50%;text-align:center;color:white;font-size:12px;cursor: pointer;">x</div>'
html+='<img src="'+this.result+'" style="width: 100%;height:100%"/></div>'
$('.addImgShow').append(html);
};
})(e.target.files[0]);
reader.readAsDataURL(e.target.files[0]);
})

 

var show_img_length=$('.show_img').length;
console.log(show_img_length);
for (var i=0;i<show_img_length;i++) {
var base64 = $('.show_img').eq(i).find('img').attr('src');
//压入图片
data.push({"name":"img"+i,"value":base64})
}

然后异步就可以发送数据了,

后天对这个数据进行处理保存一下就可以了

 

后台处理方法

/*
* base64上传图片
*/
public function uploadImg($base64=null,$dir=null){

if(is_array($base64)){
$new_files;
if($dir){
$path = $dir;
}else{
$path = "./Public/upload/goods/".date('Y').'/'.date('m-d').'/';
}
is_dir($path) or mkdir($path,0777,true);

foreach($base64 as $k=>$v){

if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $v, $result)){
$type = $result[2];

$new_file = $path.md5($v).".{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $v)))){
$new_files[]=$new_file;

}
}
}
return $new_files;
exit;
}else{

if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
$type = $result[2];

if($dir){
$path = $dir;
}else{
$path = "./Public/upload/goods/".date('Y').'/'.date('m-d').'/';
}

is_dir($path) or mkdir($path,0777,true);
$new_file = $path.md5(time()).".{$type}";

if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64)))){
return $new_file;
exit;
}
}
}

}

转载于:https://www.cnblogs.com/globalfy/p/7929805.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值