如何把src为base64格式的img上传到服务器
现在页面上有<imgsrc="data:image/png;base64,...">我想把它赋值到一个input[file]上,然后提交到服务器这个赋值的步骤怎么弄...
现在页面上有<img src="data:image/png;base64,...">
我想把它赋值到一个input[file]上,然后提交到服务器
这个赋值的步骤怎么弄 展开
我想把它赋值到一个input[file]上,然后提交到服务器
这个赋值的步骤怎么弄 展开
1个回答
展开全部
我也正在做,一起研究吧
$.post("/unifiedtool/uploadProductImg", { "img": “data:image/png;base64,...
”},function(ret){
if(ret.img!=""){
img.src = ret;
}else{
alert("upload fail");
}
});
服务器那边
String imgString =request.getParameter("img");
UserBean user = (UserBean)request.getSession().getAttribute("user") ;
String language_id = request.getParameter("language_id");
//对字节数组字符串进行Base64解码并生成图片
if (imgString == null) //图像数据为空
return "img error";
imgString = imgString.substring(imgString.indexOf(",")+1, imgString.length());
String imgFilePath = "";
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(imgString);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
File newPath = new File(request.getSession().getServletContext().getRealPath("/")+"download/"+user.getUserid()+"/product/"+language_id+"/0");
if(!newPath.exists()){
newPath.mkdirs();
imgFilePath = "1.jpg";
}else{
if(newPath.list().length > 0){
imgFilePath = (newPath.list().length+1)+".jpg";
}else{
imgFilePath = "1.jpg";
}
}
//String imgFilePath = "d:\\1111.jpg";//新生成的图片
OutputStream out = new FileOutputStream(newPath+"/"+imgFilePath);
out.write(b);
out.flush();
out.close();
}
catch (Exception e)
{
return "img error";
}
return "/download/"+user.getUserid()+"/product/"+language_id+"/0/"+imgFilePath;
ok
$.post("/unifiedtool/uploadProductImg", { "img": “data:image/png;base64,...
”},function(ret){
if(ret.img!=""){
img.src = ret;
}else{
alert("upload fail");
}
});
服务器那边
String imgString =request.getParameter("img");
UserBean user = (UserBean)request.getSession().getAttribute("user") ;
String language_id = request.getParameter("language_id");
//对字节数组字符串进行Base64解码并生成图片
if (imgString == null) //图像数据为空
return "img error";
imgString = imgString.substring(imgString.indexOf(",")+1, imgString.length());
String imgFilePath = "";
BASE64Decoder decoder = new BASE64Decoder();
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(imgString);
for(int i=0;i<b.length;++i)
{
if(b[i]<0)
{//调整异常数据
b[i]+=256;
}
}
//生成jpeg图片
File newPath = new File(request.getSession().getServletContext().getRealPath("/")+"download/"+user.getUserid()+"/product/"+language_id+"/0");
if(!newPath.exists()){
newPath.mkdirs();
imgFilePath = "1.jpg";
}else{
if(newPath.list().length > 0){
imgFilePath = (newPath.list().length+1)+".jpg";
}else{
imgFilePath = "1.jpg";
}
}
//String imgFilePath = "d:\\1111.jpg";//新生成的图片
OutputStream out = new FileOutputStream(newPath+"/"+imgFilePath);
out.write(b);
out.flush();
out.close();
}
catch (Exception e)
{
return "img error";
}
return "/download/"+user.getUserid()+"/product/"+language_id+"/0/"+imgFilePath;
ok
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询