使用input file不需上传服务器选择后实现预览

$("input:file").change(function(){
	  var thisObj = this;
	  var reader = new FileReader();
	  reader.onload = function (e) {
		  var data = e.target.result;
		  //加载图片获取图片真实宽度和高度
		  var image = new Image();
		  image.onload=function(){
		      var width = image.width;
		      var height = image.height;
			  $("img.showImageImg").attr("src",reader.result);
		  };
	      image.src= data;
	 };
	 reader.readAsDataURL(this.files[0]);
  });

   reader.result 得到BASE64节码

 可以通过传输BASE64实现文件上传服务器

 

/**
	 * 
	 * @param base64Str 
	 * @throws IOException
	 */
	public static void uploadBASE64(String base64Str) throws IOException{
		BASE64Decoder d = new BASE64Decoder(); 
		//data:image/gif;base64,R0lGODlhg.....
		//去除逗号和逗号之前字符串
        byte[] bs = d.decodeBuffer(base64Str);  
        FileOutputStream os = new FileOutputStream("xxxx/x.gif");  
        os.write(bs);  
        os.close();
	}

 

猜你喜欢

转载自zsjdxc251.iteye.com/blog/2363323