コード例 #1
0
        public ActionResult UpdateImage(Models.ImageFileModel imageModel, HttpPostedFileBase image_update)
        {
            try
            {
                if (image_update != null && image_update.ContentLength > 0)
                {
                    int      MaxContentLength      = 1024 * 1024 * 3;
                    string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".pdf" };

                    if (!AllowedFileExtensions.Contains(image_update.FileName.Substring(image_update.FileName.LastIndexOf('.'))))
                    {
                        ModelState.AddModelError("update_Image", "Please only use file types: " + string.Join(", ", AllowedFileExtensions));
                    }
                    else if (image_update.ContentLength > MaxContentLength)
                    {
                        ModelState.AddModelError("update_Image", string.Format("Your file is too large, maximum file size is: {0} Bytes.", MaxContentLength));
                    }
                    else
                    {
                        byte[] picture_bytes = new byte[image_update.ContentLength];
                        image_update.InputStream.Read(picture_bytes, 0, image_update.ContentLength);
                        imageModel.ImageData = picture_bytes;
                        imageModel.UpdateImage();
                    }
                }
            }
            catch (Exception e)
            {
            }
            return(Redirect(Request.UrlReferrer.ToString()));
        }
コード例 #2
0
        public ActionResult UploadImage(Models.ImageFileModel imageModel, HttpPostedFileBase image_upload)
        {
            string exception_message = string.Empty;
            string details           = string.Empty;

            try
            {
                if (image_upload != null && image_upload.ContentLength > 0)
                {
                    int      MaxContentLength      = 1024 * 1024 * 3;
                    string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".pdf" };

                    if (!AllowedFileExtensions.Contains(image_upload.FileName.Substring(image_upload.FileName.LastIndexOf('.'))))
                    {
                        //ModelState.AddModelError("upload_Image", "Please only use file types: " + string.Join(", ", AllowedFileExtensions));
                        exception_message += "Please only use file types: " + string.Join(", ", AllowedFileExtensions);
                        details           += "File was type: " + image_upload.FileName.Substring(image_upload.FileName.LastIndexOf('.'));
                    }
                    else if (image_upload.ContentLength > MaxContentLength)
                    {
                        //ModelState.AddModelError("upload_Image", string.Format("Your file is too large, maximum file size is: {0} Bytes.", MaxContentLength));
                        exception_message += string.Format("Your file is too large, maximum file size is: {0} Bytes.", MaxContentLength);
                        details           += "File is too large";
                    }
                    else
                    {
                        byte [] picture_bytes = new byte[image_upload.ContentLength];
                        image_upload.InputStream.Read(picture_bytes, 0, image_upload.ContentLength);
                        imageModel.ImageData = picture_bytes;
                        imageModel.UploadImage();
                    }
                }
            }
            catch (Exception e)
            {
            }
            if (!string.IsNullOrEmpty(exception_message))
            {
                return(Json(new { success = false, code = exception_message, message = details }));
            }
            else
            {
                return(Redirect(Request.UrlReferrer.ToString()));
            }
        }