Uploadpropic Controller

    #region 上传到服务器图片
        [HttpPost]
        public JsonResult Uploadpropic()
        {
            string msg = string.Empty;
            string filePath = string.Empty;
            string success = "0";
            var fileList = Request.Files;
            if (fileList.Count > 0)
            {
                HttpPostedFileBase file = Request.Files[0];
                int lenth = file.ContentLength;
                if (lenth < 3000000)
                {
                    string extFile = System.IO.Path.GetExtension(file.FileName);
                    if (!string.IsNullOrWhiteSpace(extFile))
                    {
                        extFile = extFile.ToLower();
                        if (extFile == ".jpg" || extFile == ".png" || extFile == ".jpeg")
                        {
                            filePath = "../upload/" + setRandomstr() + extFile;
                            string filesyspath = System.IO.Path.Combine(Server.MapPath(filePath));
                            using (Stream fileStream = file.InputStream)
                            {
                                using (Bitmap bitmap = ImageHelper.GetThumImage(fileStream))
                                {
                                    if (bitmap != null)
                                    {
                                        bitmap.Save(filesyspath, ImageFormat.Jpeg);
                                        msg = "图片上传成功";
                                        success = "1";
                                    }
                                    else
                                    {
                                        msg = "图片上传失败,请重试";
                                    }
                                }
                            }
                        }
                        else
                        {
                            msg = "只允许上传jpg|jpeg|png格式的图片";
                        }
                    }
                    else
                    {
                        msg = "上传的图片格式不正确";
                    }
                }
                else
                {
                    msg = "请将图片大小修改为3M以内";
                }

            }
            else
            {
                msg = "请选择要上传的图片";
            }

            return Json(new { result = success, path = filePath, msg = msg }, JsonRequestBehavior.AllowGet);
        }
        #endregion

 

posted @ 2017-05-15 13:14  冫凌晨  阅读(253)  评论(0编辑  收藏  举报