<img : ="errpic" class="customerHead" :src="param.customerHead" alt="">
data() {
return {
param:{
id:"",
customerHead: "",
}
}
}
let _this = this
let files = e.target.files[0]
if (files.size/(1024*1024) > 2) {
this.open('上传的图片不可大于2M!')
return false;
}
var reader = new FileReader();
reader. = function (e) {
var 64 = e.target.result;
_this.param.customerHead = 64
//console.log( 64)
}
if(files) {
reader.readAsDataURL(files);
}

如果修改头像,向后台传 64字符串,否则会传原图片路径,后台判断是否是 64字符串.
如果是 64字符串,则对 64字符串进行处理,在后台服务器生成图片.此处需要对 64字符串进行处理,如图所示,删除蓝框部分,留逗号之后的内容.
若为图片路径,则不需要进行处理,直接返回图片路径即可.

@Value("${upload.image.path}")
private String filePath;
// 64字符串转化成图片 headerImgPath:http://+ip+:端口号
public String generateImage(String imgStr,String headerImgPath,String cusID)
{ //对字节数组字符串进行 64解码并生成图片
if (imgStr == null) //图像数据为空
return "../picclife/static/custom.png";
64Decoder decoder = new 64Decoder();
try
{
//判断是 64字符串还是图片路径
if(imgStr.substring(0,5).equals("data:")){
// 64解码
byte[] b = decoder.decodeBuffer(imgStr.substring(imgStr.indexOf(",") + 1));
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成图片
String imgFilePath = filePath+"/headerImg/"+cusID+".jpg";//新生成的图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
return headerImgPath+"headerImg/"+cusID+".jpg";
}else{
return imgStr;
}
}
catch (Exception e)
{
return "../picclife/static/custom.png";
}
}


总结
以上所述是小编给大家介绍的vue项目 64字符串转图片的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
继续阅读与本文标签相同的文章
上一篇 :
AI降噪软件出现,手机双麦克风时代或被终结?
-
特斯拉国产车型上市在即,自主电动车压力山大
2026-05-15栏目: 教程
-
群呼短信功能参数的维护方法
2026-05-15栏目: 教程
-
一分钟搞定堆积柱状图的合计标签
2026-05-15栏目: 教程
-
华为发布5G全系列解决方案
2026-05-15栏目: 教程
-
海宁国际智能缝制设备展助力千亿级纺织服装产业集群
2026-05-15栏目: 教程
