Exemple #1
0
        public ActionResult UpLoadImage()
        {
            BaseResult br = new BaseResult();

            try
            {
                HttpPostedFileBase postedFile = Request.Files.Count > 0 ? Request.Files[0] : null;
                if (postedFile == null || postedFile.ContentLength <= 0)
                {
                    br.Success = false;
                    br.Level   = ErrorLevel.Error;
                    br.Message.Add("<h5>上传失败</h5>");
                    br.Message.Add("");
                    br.Message.Add("未发现上传的文件!");
                    return(JsonString(br));
                }
                if (!CyVerify.IsImage(postedFile.InputStream))
                {
                    br.Success = false;
                    br.Message.Add("<h5>上传失败</h5>");
                    br.Message.Add("");
                    br.Message.Add("上传的文件格式不正确,必须图片文件!");
                    br.Level = ErrorLevel.Warning;
                    return(JsonString(br));
                }
                string extension    = Path.GetExtension(postedFile.FileName);
                string fileName     = DateTime.Now.ToString("yyyyMMddHHmmssfff") + extension;
                string url          = "/UpLoad/Temp/" + fileName;
                string fileFullName = ApplicationInfo.TempPath + "\\" + fileName;
                if (!Directory.Exists(ApplicationInfo.TempPath))
                {
                    Directory.CreateDirectory(ApplicationInfo.TempPath);
                }

                postedFile.SaveAs(fileFullName);
                br.Data = url;
                br.Message.Add("上传成功!");
                br.Success = true;

                //移除过期文件
                FileHelper.FilesClear(ApplicationInfo.TempPath, TimeSpan.FromDays(1));
            }
            catch (CySoftException cex)
            {
                br = cex.Result;
            }
            catch (Exception)
            {
                br.Success = false;
                br.Level   = ErrorLevel.Error;
                br.Message.Add("<h5>上传失败</h5>");
                br.Message.Add("");
                br.Message.Add("请重试或与管理员联系!");
            }
            return(Content(JSON.Serialize(br)));
        }
Exemple #2
0
        public ActionResult UploadPic(string obj)
        {
            BaseResult  br    = new BaseResult();
            Hashtable   param = JSON.Deserialize <Hashtable>(obj);
            ParamVessel p     = new ParamVessel();

            p.Add("id_master", (long)0, HandleType.ReturnMsg);
            param = param.Trim(p);
            try
            {
                HttpPostedFileBase postedFile = Request.Files.Count > 0 ? Request.Files[0] : null;
                if (postedFile == null || postedFile.ContentLength <= 0)
                {
                    br.Success = false;
                    br.Level   = ErrorLevel.Error;
                    br.Message.Add("<h5>上传失败</h5>");
                    br.Message.Add("");
                    br.Message.Add("未发现上传的文件!");
                    return(Json(br));
                }
                if (!CyVerify.IsImage(postedFile.InputStream))
                {
                    br.Success = false;
                    br.Message.Add("<h5>上传失败</h5>");
                    br.Message.Add("");
                    br.Message.Add("上传的文件格式不正确,必须图片文件!");
                    br.Level = ErrorLevel.Warning;
                    return(Json(br));
                }
                Tb_User_Pic pic          = new Tb_User_Pic();
                string      guid         = Guid.NewGuid().ToString();
                string      extension    = Path.GetExtension(postedFile.FileName);
                string      fileName     = guid + extension;                  //文件名
                string      url          = "/UpLoad/User/Master/" + fileName; //存入数据库中的图片路径
                string      fileFullName = ApplicationInfo.UserMasterPath + "\\" + fileName;
                if (!Directory.Exists(ApplicationInfo.UserMasterPath))
                {
                    Directory.CreateDirectory(ApplicationInfo.UserMasterPath);
                }
                //上传原图
                postedFile.SaveAs(fileFullName);
                //生成缩略图至UpLoad/Master下
                string minUrl = "/UpLoad/User/Master/_480x480_" + fileName;
                ImgExtension.MakeThumbnail(url, minUrl, 480, 480, ImgCreateWay.Cut, false);
                pic.id_master = long.Parse(param["id_master"].ToString());
                pic.id_create = GetLoginInfo <long>("id_user");
                pic.photo     = url;
                pic.photo_min = minUrl;
                br            = BusinessFactory.UserPic.Add(pic);
                br.Data       = url;
                br.Success    = true;
            }
            catch (CySoftException cex)
            {
                br = cex.Result;
            }
            catch (Exception)
            {
                br.Success = false;
                br.Level   = ErrorLevel.Error;
                br.Message.Add("<h5>上传失败</h5>");
                br.Message.Add("");
                br.Message.Add("请重试或与管理员联系!");
            }
            return(Json(br));
        }