行一恒  


    <div class="photo" style="display:none;" id="upPhoto"><div class="pop-cover"></div><div style="position: absolute; top:30%; left:5%; width:90%; margin:0 auto; z-index:99;">
<a href="#" class="potos tbtm" ><input type="file" name="file" id="file1"  style="display:block; width:100%; text-align:center; opacity: 0;  cursor:pointer;"/><span style=" z-index:1; position:relative; top:-8px; ">本地上传</span></a><a href="#" class="cancel">取消</a></div></div>  
    </div>
  </section>
   </div>
    </form>
</body>
<script type="text/javascript" src="js/lib/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="js/lib/ajaxfileupload.js"></script>
<script>

    $(document).ready (function () {

    $("#head").click(function () {
        $(".photo").show();

    });
    $("#file1").on("change", function () {
        change();
    })
    function change(target) {
        $(".photo").hide();
        var allowExtention = ".jpg,.bmp,.png,.jpeg";
       // console.log(target);
        var input = document.getElementById("file1"); //获取input标签
        var extention = input.value.substring(input.value.lastIndexOf(".") + 1).toLowerCase(); //截取后缀
        if (extention && allowExtention.indexOf(extention) > -1) {  //判断上传图片类型
            var fileSize = input.files[0].size; //获取上传图片大小
            if (fileSize) {
                if (fileSize <1.5* 1000 * 1024) {  //js限制图片上传大小
                    ajaxFileUpload();
                }
                else
                    addSystemTip($page, "上传图片过大!");
            }

        }
        else
            addSystemTip($page, "上传图片格式不正确!");

    }

 

    // 使用jquery ajaxFileupload插件 原理是把一个form提交到后端来获取图片

   //所以 input type=file 标签必须要给一个 name属性,是必须的否则后台无法获取图片
    function ajaxFileUpload() {
        $.ajaxFileUpload({
            url: "MyAccount.aspx?type=img"////用于文件上传的服务器端请求地址
            , secureuri: false // //是否需要安全协议,一般设置为false
            , fileElementId: 'file1' //文件上传域的ID
            , dataType: 'json' //返回值类型 一般设置为json
          //  , type: 'post'
            , success: function (data, status)  //服务器成功响应处理函数
            {                             
                $("#img").attr("src", data.imgurl);
                //  var input = document.getElementById("file1");
                var span = $("#file1").next();
                $("#file1").remove();   //上传成功后删除file标签,否则无法继续上传          
                var ele = $('<input type="file" name="file"  id="file1" style="display:block; width:100%; text-align:center; opacity: 0;  cursor:pointer;"/>'); //重新创建file标签
                ele.insertBefore(span);
                ele.on("change", function () { //重新绑定change事件句柄
                    change();
                })            
                $(".photo").hide();
            },
            error: function (data, status, e)//服务器响应失败处理函数
            {
                $(".photo").hide();
            }
        });        
    }
    $(".cancel").click(function () {

        $(".photo").hide();

    })
    $(".pop-cover").mousemove(function () {

        $(".photo").hide();

    })
})
</script>
</html>

 

//对应后台代码,并且将上传图片重新绘画一下 指定像素大小

    public partial class MyAccount : MemberBase
    {
        public MemberDetails user = new MemberDetails();
        
        public string ImageURl = "";
        public string encodeName = "";
        public string sex = "保密";
        string domain = XpVShop.MyFuc.Utils.GetAppSettingValue("turnUrl") + "/Upload/users/";
        string localUrl = XpVShop.MyFuc.Utils.GetAppSettingValue("PCUploadPath2");

       // string domain = "http://localhost:89/Upload/images/";//本地测试使用
       // string localUrl=@"F:\hz\code\aishop\Publish\vshop\Upload\images\";
      
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                XpVShop.MyFuc.LogHelper.Write("MyAccount mid:" + CurrentUser.MemberID);
                user = MemberBLL.GetMemberDetails(CurrentUser.MemberID);
                string u = "vshop_ChangeName.aspx";
                encodeName = XpVShop.MyFuc.Utils.UrlEncode(u);
                if (user.Photo.IndexOf("http://wx.qlogo.cn") == -1)
                    ImageURl = domain + user.Photo;
                else
                    ImageURl = user.Photo;
                if (!string.IsNullOrEmpty(user.Mobile))
                    user.Mobile = user.Mobile.Substring(0, 5) + "***" + user.Mobile.Substring(user.Mobile.Length - 1, 1);
                ;
                if (user.Sex) sex = "男";
                else sex = "女";
                string type = Utils.ReqStrParams("type", "");
                if (type == "img")
                {
                    HttpFileCollection files = Request.Files;
                    string msg = string.Empty;
                    string error = string.Empty;
                    string imgurl;

                    if (files.Count > 0)
                    {
                        string name = System.IO.Path.GetFileName(files[0].FileName);
                        // string url = Server.MapPath("Upload/images/") + CurrentUser.MemberID + name;  

                        XpVShop.MyFuc.LogHelper.Write(" MyAccount 存放路径 localUrl:" + localUrl);
                        try
                        {
                            string strGuid = System.Guid.NewGuid().ToString().ToUpper(); //始化 Guid 类的新
                            string strFileExt = name.Substring(name.LastIndexOf(".")); //得到图片的后缀
                            string path = localUrl + strGuid  + strFileExt;
                            System.Web.HttpPostedFile postedFile = files[0];
                            XpVShop.MyFuc.LogHelper.Write("MyAccount 存放地址之前;" + path);                        
                           // files[0].SaveAs(localUrl + CurrentUser.MemberID + name);
                            XpVShop.MyFuc.Utils.GetThumbNail(name, 350,200, "image/pjpeg", false, postedFile.InputStream, path);
                            imgurl = strGuid + strFileExt;
                            user.Photo = imgurl;                        
                            MemberBLL.UpdateMemberPhoto(user);
                            CurrentUser.Photo = imgurl;
                            ImageURl = domain + user.Photo;
                            XpVShop.MyFuc.LogHelper.Write(" MyAccount 保存图片路径" + ImageURl + "保存数据库字段:user.Photo:" + imgurl + "存放地址 " + path);
                            // msg = " 成功! 文件大小为:" + files[0].ContentLength;                                              
                            string res = "{ error:'" + error + "', msg:'" + msg + "',imgurl:'" + ImageURl + "'}";
                            Response.Write(res);
                            Response.End();
                        }
                        catch (Exception ex)
                        {
                            XpVShop.MyFuc.LogHelper.Write("MyAccount ex:" + ex.ToString());

                        }

                    }
                }
            }

        }     
    }

 

            /// <summary>
           /// 处理上传图片为指定像素
           /// </summary>
           /// <param name="strFileName">文件名</param>
           /// <param name="iWidth"></param>
           /// <param name="iheight"></param>
           /// <param name="strContentType">图片类型</param>
           /// <param name="blnGetFromFile">是否是从本地文件名获取文件流</param>
           /// <param name="ImgStream">文件流</param>
           ///  <param name="path">保存路径</param>
           public static void GetThumbNail(string strFileName, int iWidth, int iheight, string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream, string path)
           {
               System.Drawing.Image oImg;
               if (blnGetFromFile) oImg = System.Drawing.Image.FromFile(strFileName);
               oImg = System.Drawing.Image.FromStream(ImgStream);
               oImg = oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero);   //GetThumbnailImage方法是返回此Image对象的缩略图
              // string strGuid = System.Guid.NewGuid().ToString().ToUpper(); //始化 Guid 类的新
              // string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));  //得到图片的后缀
               //MemoryStream MemStream = new MemoryStream(); //创建其支持存储区为内存的流     
               oImg.Save(path);             
           }

posted on 2015-07-24 17:58  行一恒  阅读(596)  评论(0编辑  收藏  举报