js转图片为base64上传

  • 分类:【前端
  • 浏览【49】
  • 评论【0】
  • 更新【2018-6-25 15:10:28】

//上传图片
	var imagesBase64 = [];
	$("input[type=file]").on("change", function (e) {
		var image = e.target.files[0];
		var imgFile = new FileReader();
		var imgShow = new Image();
		imgFile.readAsDataURL(image);
		imgFile.onload = function () {
			var imgData = this.result; //base64数据
			imagesBase64.push({
				teaId: teacherInfo.id,
				fileType: "award",//图片
				fileName: image.name,
				file: imgData,
			});
			imgShow.src = imgData;
			imgShow.style.width = "100px";
			imgShow.style.height = "100px";
			imgShow.title = "双击删除";
			$("#imageShow").append(imgShow);
		}
	});
	$("#imageShow").on("dblclick ", "img", function () {
		var index = $("#imageShow > img").index(this);
		console.log(index);
		$(this).remove();
		imagesBase64.splice(index);
		console.log(imagesBase64)
	});

关键字词:图片转base64