H5上传图片之canvas
2018-05-07 11:52
375 查看
H5上传图片之canvas,使用canvas处理压缩图片再上传
html代码:
<form action="" method="post"> <dl> <dd> <div> <div id="img_wrap"> </div> <input type="file" accept="image/jpg,image/jpeg,image/png,image/gif" id="fileImage" multiple="multiple" name="img" style="display:none" /> <a id="upimg" href="javascript:">上传图片</a> </div> </dd> <dd style="margin-top:10px;"> <a id="abtn_submit" href="javascript:">提交</a> </dd> </dl> </form>
js代码:
<script type="text/javascript"> $(function () { $("#abtn_submit").click(function () { if ($("input[name='imgs']").length < 1) { layer.open({ content: '必须上传图片才能提交', skin: 'msg', time: 2 }); return; } $("form").submit(); }); $("#upimg").click(function () { $("#fileImage").click(); }) $("#fileImage").change(function () { $.each($(this)[0].files, function (i, e) { if (!/image\/\w+/.test(e.type)) { //请确保文件为图像类型 return; } imgHandle(e); }) }) }); function imgHandle(_file) { var reader = new FileReader(); reader.readAsDataURL(_file); reader.onload = function (e) { var result = e.target.result; var image = new Image(); image.src = result; image.onload = function () { var cvs = document.createElement("canvas"); var scale = 1; if (this.width > 1000 || this.height > 1000) { if (this.width > this.height) { scale = 1000 / this.width; } else { scale = 1000 / this.height; } } cvs.width = this.width * scale; cvs.height = this.height * scale; var ctx = cvs.getContext("2d"); ctx.drawImage(this, 0, 0, cvs.width, cvs.height); var newImageData = cvs.toDataURL(_file.type, 0.8); var imgdata = "<a href='javascript:' onclick='delimg(this)'><img src=\"" + newImageData + "\" />"; imgdata += "<input type=\"hidden\" name=\"imgs\" value=\"" + newImageData + "\" /></a>"; $("#img_wrap").append(imgdata); } } } function delimg(e) { layer.open({ content: '您确定要删除此图片吗?', btn: ['删除', '取消'], skin: 'footer', yes: function (index) { $(e).remove(); layer.close(index); } }); } </script>
后台处理代码:
//有图片时处理如下 string[] imgs = collection.GetValues("imgs"); List<U_Img> uimglist = new List<U_Img>(); int i = 1; foreach (string imgBase64Str in imgs) { int indexOf = imgBase64Str.IndexOf('/') + 1; string ftype = imgBase64Str.Substring(indexOf, imgBase64Str.IndexOf(';') - indexOf); string fex = ".jpg"; switch (ftype) { case "jpeg": case "jpg": fex = ".jpg"; break; case "png": fex = ".png"; break; case "gif": fex = ".gif"; break; } string Base64Str = imgBase64Str.Substring(imgBase64Str.IndexOf(',') + 1); Base64Str = Base64Str.Trim('\0'); byte[] imgArr = Convert.FromBase64String(Base64Str); using (MemoryStream ms = new MemoryStream(imgArr)) { Bitmap bmp = new Bitmap(ms); string fname = "/timg/" + DateTime.Now.ToString("yyMMddmmHHssffff") + i.ToString() + fex; string FilePath = Request.MapPath(fname); bmp.Save(FilePath); uimglist.Add(new U_Img() { ImgUrl = fname, ImgTime = DateTime.Now }); } i++; }
相关文章推荐
- H5使用Base64和Canvas上传图片
- H5上传图片并使用canvas制作海报
- h5 canvas 图片上传操作
- 利用H5Canvas进行前端图片压缩再上传笔记
- h5 canvas实现图片裁剪上传
- H5 canvas实现客户端压缩图片上传
- js移动端/H5同时选择多张图片上传并使用canvas压缩图片
- 前端预览图片和H5canvas压缩图片上传
- JS移动端/H5同时选择多张图片上传并使用canvas压缩图片
- H5上传图片并使用canvas制作海报
- 让Android的webview支持H5的图片上传,无需原生开发功能
- 前台图片Canvas压缩上传小结
- HTML5+Canvas手机拍摄,本地压缩上传图片,案例实测报告。
- 通过Canvas及File API缩放并上传图片
- 移动端H5上传图片并压缩上传
- 【H5】手机浏览器分批次多张上传图片(文件),包含进度条
- H5压缩图片 AJAX上传图片
- HTML5开发笔记:初窥CANVAS,上传canvas图片到服务器
- h5上传图片及预览
- 通过Canvas及File API缩放并上传图片完整示例