コード例 #1
0
        public void DoUploadImg()
        {
            HttpFileCollection fileCollection = System.Web.HttpContext.Current.Request.Files;//文件列表
            var id = RequestParameters.PGuid("txtId2");

            if (fileCollection.Count > 0)
            {
                HttpPostedFile file = fileCollection[0];

                //当前文件后缀名
                string ext = System.IO.Path.GetExtension(file.FileName).ToLower();
                //验证文件类型是否正确
                string[] arrayExtension = { ".gif", ".jpg", ".jpeg", ".png", ".bmp" };
                if (!arrayExtension.Contains(ext))
                {
                    Response.Write("<script>window.parent.ImgUpload_Callback('-3');</script>");
                    return;
                }
                //验证文件的大小
                if (file.ContentLength > 1024 * 1024 * 10)//10M
                {
                    Response.Write("<script>window.parent.ImgUpload_Callback('-4');</script>");
                    return;
                }
                //当前文件上传的目录
                string _fileSavePath = Server.MapPath("/Upload/CustomerHeadImg/");
                //当前待上传的服务端路径
                string _fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ext;
                //开始上传
                try
                {
                    if (!System.IO.Directory.Exists(_fileSavePath))
                    {
                        System.IO.Directory.CreateDirectory(_fileSavePath);
                    }

                    file.SaveAs(_fileSavePath + _fileName);
                    var cBll    = new CustomerBll();
                    var cusItem = cBll.GetObjectById(id);
                    if (cusItem != null)
                    {
                        Guid customerid      = cusItem.ID;
                        Guid welfareCentreId = cusItem.WelfareCentreID ?? Utits.WelfareCentreID;
                        var  isFlag          = cBll.UpdateCustomerHeadImg(customerid, welfareCentreId, _fileName);
                        if (isFlag == true)
                        {
                            Response.Write("<script>window.parent.ImgUpload_Callback('1|" + _fileName + "');</script>");
                        }
                        else
                        {
                            Response.Write("<script>window.parent.ImgUpload_Callback('-1');</script>");
                        }
                    }
                }
                catch
                {
                    //上传失败
                    Response.Write("<script>window.parent.ImgUpload_Callback('-1');</script>");
                    return;
                }
                //这里window.parent.PictureUpload_Callback()是我在前端页面中写好的javascript function,此方法主要用于输出异常和上传成功后的图片地址
                //如果成功返回的数据是需要返回两个字符串,我在这里使用了|分隔  例: 成功信息|/Test/hello.jpg
                Response.Write("<script>window.parent.ImgUpload_Callback('1|" + _fileName + "');</script>");
            }
            else
            {
                //上传失败
                Response.Write("<script>window.parent.ImgUpload_Callback('-1');</script>");
            }
        }