예제 #1
0
        /// <summary>
        /// 上傳圖片
        /// </summary>

        /// <returns></returns>
        public ActionResult ImageUpload(String strProfileID)
        {
            //ViewBag.strProductID = strProductID;
            ViewBag.strProfileID = strProfileID;
            ViewBag.SizeLimit    = ImageUploadHelper.GetProfileParm("SizeLimit");
            return(View());
        }
예제 #2
0
        /// <summary>
        /// 圖片上傳頁面
        /// </summary>
        ///
        /// <returns></returns>
        public ActionResult Upload()
        {
            //   ProductMaitainModel ProductData = new ProductMaitainModel();
            //if (!String.IsNullOrEmpty(strProductID))
            //{
            // ViewBag.PictureID  = ImageUploadHelper.GetProductData();
            //}
            //ProductData.strProductID = strProductID;

            //取得圖片尺寸&限制大小
            ViewBag.Picture = ImageUploadHelper.GetProfileParm("Picture");
            //ViewBag.BackgroundPicture = ImageUploadHelper.GetProfileParm("BackgroundPicture");
            ViewBag.SizeLimit = ImageUploadHelper.GetProfileParm("SizeLimit");
            return(View());
        }
        public void ProcessRequest(HttpContext context)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();

            try
            {
                //取得檔案放置路徑
                var uploadPath = context.Request.Params["uploadPath"];
                //取得上傳圖片類型尺寸
                var          strProfileID    = context.Request.Params["strProfileID"];
                PhysicalFile objPhysicalFile = new PhysicalFile();
                //取得會使用到的相關資訊
                //objArticleService = new ArticleService();
                //Enum.TryParse(sArticleStatus, out objArticleStatus);
                //objArticle = objArticleService.GetArticle(sAnswerCode, objArticleStatus);
                //objSystemParam = new SysemParam();

                //壓縮檔處理
                foreach (string file in context.Request.Files)
                {
                    HttpPostedFile hpf = context.Request.Files[file] as HttpPostedFile;

                    //fileExtension:副檔名
                    String fileExtension = System.IO.Path.GetExtension(hpf.FileName).ToLower();

                    object result       = new object();
                    string sZipFilePath = string.Empty;
                    string sFolderPath  = string.Empty;

                    Image img = Image.FromStream(hpf.InputStream, true, true);

                    //取得影像的格式
                    ImageFormat thisFormat = img.RawFormat;

                    String   PictureSize      = ImageUploadHelper.GetProfileParm(strProfileID);
                    String[] PictureSizeLimit = PictureSize.Replace(" ", "").Split('x');
                    //寬 & 高不得小於設定
                    //if (img.Width < Convert.ToInt32(PictureSizeLimit[0]) || img.Height < Convert.ToInt32(PictureSizeLimit[1]))
                    //{
                    //    String ErrorMsg = "請上傳尺寸大於" + PictureSize + "的檔案,謝謝!";
                    //    result = new { status = "Failure", ErrorMsg = ErrorMsg };
                    //    var jsonObjImg = js.Serialize(result);
                    //    context.Response.Write(jsonObjImg.ToString());
                    //    break;
                    //}

                    if (hpf.ContentLength == 0)
                    {
                        continue;
                    }

                    //資料夾路徑
                    sFolderPath = System.Web.HttpContext.Current.Server.MapPath(uploadPath + "/");
                    List <MultiSizeImg> MultiSizeImgList = new List <MultiSizeImg>();

                    if (String.Equals(strProfileID, "Picture"))
                    {
                        MultiSizeImgList = ResizeImg(img, new String[] { "MiddlePicture", "SmallPicture" }, sFolderPath, fileExtension);
                    }
                    else
                    {
                        MultiSizeImgList = ResizeImg(img, new String[] { "BackgroundPicture" }, sFolderPath, fileExtension);
                    }

                    //要儲存的路徑
                    objPhysicalFile.CreateFolder(sFolderPath);

                    foreach (var item in MultiSizeImgList)
                    {
                        //儲存圖片,需加入影像的格式,不然IE圖片不出現
                        item.ImageOut.Save(item.FilePath, thisFormat);
                    }

                    result = new { status = "Success", MultiSizeImgList = MultiSizeImgList, uploadPath = uploadPath, FolderPath = sFolderPath };
                    var jsonObj = js.Serialize(result);
                    context.Response.Write(jsonObj.ToString());
                }
            }
            catch (Exception ex)
            {
                object result     = new { status = "Failure", ErrorMsg = "發生異常錯誤,請重新上傳" };
                var    jsonObjImg = js.Serialize(result);
                context.Response.Write(jsonObjImg.ToString());
            }
        }