.net MVC + Bootstrap 下使用 localResizeIMG上传图片

本文介绍了一个基于HTML和JavaScript的图片上传组件,该组件允许用户直接从摄像头拍摄照片并上传。文章详细展示了如何使用localResizeIMG插件进行图片压缩,并通过AJAX将压缩后的Base64字符串发送到服务器端。同时,提供了服务器端接收Base64字符串并转换为图片文件的方法。

需要加载的头文件

 

<div class="form-group">
            <label class="col-sm-6 control-label" for="inputfile">维修照片上传</label>
            <div class="col-sm-6 ">
                <div style="background:url(../../fonts/add.png) no-repeat;width:151px;height:150px;float:left;" id="div1">
                    <input type="file" accept="image/*" id="file" class="selectinput" style="width:151px;height:150px;opacity:.01">
                </div>

            </div>
        </div>

accept=“image/*” 这里有些手机可以拍照 有些不行 没有具体测试统计
$("#file").localResizeIMG({
        width: 400,
        //height: 200,
        quality: 1,
        success: function (result) {
            var img = new Image();
            img.src = result.base64;

            //$("body").append(img);
            $("#odd").append(img); //呈现图像(拍照結果)
            $.ajax({
                url: "/Home/UploadImg",
                type: "POST",
                data: { "formFile": result.clearBase64, "RepairNum": $('#RepairNum').val()},
                dataType: "HTML",
                timeout: 1000,
                error: function () {
                    alert("ajax Error");
                },
                success: function (data) {
                    //alert("Uploads success~")
                }
            });
        }
    });

界面样式

后台action  Base64StringToImage 需要把压缩后的Base64转换

[HttpPost]
        public ActionResult UploadImg()
        {
            var file = Request["formFile"];
            var id = Request["RepairNum"];

            string fileName = "1.jpeg";
            string filePath = Server.MapPath("~/Upload/" + fileName);

            try
            {
                Base64StringToImage(file, filePath);
                //upImg.SaveAs(filePhysicalPath);
                //Session["ImgPath"] = path;
                //Base64StringToImage(file,);
                return Content("上传成功");
            }
            catch
            {
                return Content("上传异常 !");

            }
        }

        protected void Base64StringToImage(string strbase64, string filepath)
        {
            try
            {
                byte[] arr = Convert.FromBase64String(strbase64);
                MemoryStream ms = new MemoryStream(arr);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
                //bmp.Dispose();  
                bmp.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Close();
            }
            catch (Exception ex)
            {
            }
        }

参考和下载GitHub

https://github.com/lyg945/localResizeIMG/tree/master/

 

参考博客:

http://www.cnblogs.com/manongxiaobing/p/4720568.html

http://www.cnblogs.com/52fhy/p/5355601.html

http://www.cnblogs.com/weapon-x/p/5237064.html

https://my.oschina.net/zerodeng/blog/526355?p={{totalPage}}

http://blog.youkuaiyun.com/mr_smile2014/article/details/51701674

http://blog.youkuaiyun.com/hu1991die/article/details/40585581

https://my.oschina.net/hzplay/blog/160806?p=1&temp=1492528670025#blog-comments-list

http://www.cnblogs.com/fsjohnhuang/p/3925827.html

http://www.cnblogs.com/brandonhulala/p/6080349.html

http://www.cnblogs.com/stoneniqiu/p/5957356.html

 

转载于:https://www.cnblogs.com/seanjack/p/6742767.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值