Пример #1
0
        public ActionResult GetUserImages(int num, int p)
        {
            if (UserSession.CurrentUser == null)
            {
                return(Content(""));
            }

            IList <Inpinke_Image> list = DBImageBLL.GetUserImages(PageInfo, UserSession.CurrentUser.ID);

            if (list != null)
            {
                List <pagedata> photoList = new List <pagedata>();
                foreach (Inpinke_Image i in list)
                {
                    pagedata d = new pagedata()
                    {
                        id     = i.ID,
                        bigImg = "/userfile/" + UserSession.CurrentUser.ID + "/" + i.ImageName + "_edit.jpg",
                        img    = i.Path,
                        title  = string.IsNullOrEmpty(i.Remark) ? "" : i.Remark,
                    };
                    string imgPath = Server.MapPath(d.img);
                    d.img = "/userfile/" + UserSession.CurrentUser.ID + "/" + i.ImageName + "_view.jpg";
                    string vimgPath = Server.MapPath(d.img);
                    if (!System.IO.File.Exists(imgPath))
                    {
                        continue;
                    }
                    if (!System.IO.File.Exists(vimgPath))
                    {
                        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imgPath);
                        if (originalImage != null)
                        {
                            Bitmap bitmap = new Bitmap(originalImage);
                            ImageProcessBLL.CreateStaticScaleImage(bitmap, 220, 1, 1000, vimgPath);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    photoList.Add(d);
                }
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(photoList.GetType());
                using (MemoryStream ms = new MemoryStream())
                {
                    serializer.WriteObject(ms, photoList);
                    return(Content(Encoding.UTF8.GetString(ms.ToArray())));
                }
            }
            else
            {
                return(Content(""));
            }
        }
Пример #2
0
        public ActionResult UploadFile()
        {
            HttpFileCollectionBase files = Request.Files;

            if (files.Count == 0)
            {
                Response.Write("请勿直接访问本文件");
                Response.End();
                return(Content("请勿直接访问本文件"));
            }
            string dName  = "/UserFile";
            string path   = Server.MapPath(dName);
            string userid = Request.Form["userid"];
            string bookid = Request.Form["bookid"];

            try
            {
                // 只取第 1 个文件
                HttpPostedFileBase file = files[0];
                if (file != null && file.ContentLength > 0)
                {
                    string fileName    = Request.Form["fileName"];
                    string orgFileName = fileName ?? string.Empty;

                    string[] fileNameInfo = fileName.Split('.');
                    string   extendName   = ".jpg";
                    if (fileNameInfo.Length > 1)
                    {
                        extendName = "." + fileNameInfo[fileNameInfo.Length - 1];
                    }
                    int    intRandom  = (new Random()).Next(10000, 99999);
                    string strTimeNow = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() +
                                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
                    fileName = strTimeNow + intRandom.ToString();

                    string theFileName = fileName;
                    string vPath       = dName + "/" + userid + "/OriginalImage/" + fileName + ".jpg";
                    if (!Directory.Exists(path + "/OriginalImage"))
                    {
                        Directory.CreateDirectory(path + "/" + userid + "/OriginalImage");
                    }
                    string strOriginalUrl = path + "/" + userid + "/OriginalImage/" + fileName + extendName;
                    file.SaveAs(strOriginalUrl);

                    //string mad5String = MD5HashFile.GetMD5HashFromFile(strOriginalUrl);
                    fileName = path + "/" + userid + "/" + fileName;
                    System.Drawing.Image originalImage = System.Drawing.Image.FromFile(strOriginalUrl);
                    Bitmap bitmap = new Bitmap(originalImage);
                    IList <Inpinke_ImageScale> list = DBImageScaleBLL.GetAllImageScaleSize();
                    if (list != null)
                    {
                        foreach (Inpinke_ImageScale s in list)
                        {
                            if (s.GroupID == 1)
                            {
                                ImageProcessBLL.CreateStaticScaleImage(bitmap, s.Width, s.Height, 1000, fileName + s.PlusName + ".jpg");
                            }
                            else
                            {
                                ImageProcessBLL.CreateScaleImage(bitmap, s.Width, s.Height, fileName + s.PlusName + ".jpg", s.IsScale);
                            }
                        }
                    }
                    else
                    {
                        ImageProcessBLL.CreateScaleImage(bitmap, 80, 80, fileName + "_thumb.jpg", false);
                        ImageProcessBLL.CreateScaleImage(bitmap, 2400, 2400, fileName + "_print.jpg", true);
                        ImageProcessBLL.CreateScaleImage(bitmap, 600, 600, fileName + "_edit.jpg", true);
                    }
                    originalImage.Dispose();
                    bitmap.Dispose();
                    Inpinke_Image model = new Inpinke_Image()
                    {
                        UserID       = int.Parse(userid),
                        Path         = vPath,
                        OriginalName = orgFileName.Length > 150 ? orgFileName.Substring(0, 150) : orgFileName,
                        ImageName    = theFileName,
                        HashCode     = MD5HashFile.GetMD5HashFromFile(strOriginalUrl),
                        UsedNum      = 0
                    };
                    BaseResponse br     = DBImageBLL.AddUserImage(model);
                    string       result = string.Empty;
                    if (br.IsSuccess)
                    {
                        if (!string.IsNullOrEmpty(bookid))
                        {
                            Inpinke_Book_Image bImg = new Inpinke_Book_Image()
                            {
                                UsedNum = 0,
                                ImageID = model.ID,
                                BookID  = int.Parse(bookid)
                            };
                            DBImageBLL.AddBookImage(bImg);
                        }
                        result = "{success:true,image:{id:" + model.ID + ",name:\"" + model.ImageName + "\",path:\"" + model.Path + "\"}}";
                    }
                    else
                    {
                        result = "{success:false,msg:\"" + br.Message + "\"}";
                    }
                    return(Content(result));
                }
                else
                {
                    return(Content("{success:false,msg:\"上传内容为空\"}"));
                }
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("UploadFile UserID:{0}, Error{1}", userid, ex.ToString()));
                return(Content("{success:false,msg:\"上传出错,请稍后再试\"}"));
            }
        }