private static bool SaveImage(HttpPostedFileBase fileBase, string path) { if (fileBase != null) { if (System.IO.File.Exists(path)) System.IO.File.Delete(path); try { fileBase.ResizeAndSave(Constants.ProductImageHeight, Constants.ProductImageWidth, path); return true; } catch (Exception) { return false; } } return false; }
public ActionResult UploadImage(HttpPostedFileBase imagefile, int objID) { // Получаем объект, для которого загружаем картинку // We get the object to load the image var obj = productRepository.Find(objID); if (obj == null) return View("NotFound"); try { if (imagefile != null) { // Определяем название и полный путь полноразмерной картинки и миниатюры // Determine the name and full path of a full-size images and thumbnails string strExtension = Path.GetExtension(imagefile.FileName); string strSaveFileName = objID + strExtension; string strSaveFullPath = Path.Combine(Server.MapPath(Url.Content("~/Content")), Resources.GlobalString.PRODUCT_IMAGES_FOLDER, strSaveFileName); string strSaveMiniFullPath = Path.Combine(Server.MapPath(Url.Content("~/Content")), Resources.GlobalString.PRODUCT_IMAGES_FOLDER, Resources.GlobalString.PRODUCT_IMAGES_MINI_FOLDER, strSaveFileName); // Если файлы с такими названиями уже имеются, удаляем их // If the files with names already exist, delete them if (System.IO.File.Exists(strSaveFullPath)) System.IO.File.Delete(strSaveFullPath); if (System.IO.File.Exists(strSaveMiniFullPath)) System.IO.File.Delete(strSaveMiniFullPath); // Сохраняем полную картинку и миниатюру // Retain full image and a thumbnail imagefile.ResizeAndSave(Constants.PRODUCT_IMAGE_HEIGHT, Constants.PRODUCT_IMAGE_WIDTH, strSaveFullPath); imagefile.ResizeAndSave(Constants.PRODUCT_IMAGE_MINI_HEIGHT, Constants.PRODUCT_IMAGE_MINI_WIDTH, strSaveMiniFullPath); // Расширение файла записываем в базу данных в поле ImageExt // The file extension is written to the database in the ImageExt obj.ImageExt = strExtension; productRepository.Save(); } } catch (Exception ex) { string strErrorMessage = ex.Message; if (ex.InnerException != null) strErrorMessage = string.Format("{0} --- {1}", strErrorMessage, ex.InnerException.Message); ViewBag.ErrorMessage = strErrorMessage; return RedirectToAction("ErrorPage", "Home"); } return RedirectToAction("Details", new { id = obj.ID }); }
public ActionResult UploadCatalogImage(HttpPostedFileBase imagefile, int objID) { // �������� ������, ��� �������� ��������� �������� // We get the object to load the image var obj = categoryRepository.Find(objID); if (obj == null) return View("NotFound"); try { if (imagefile != null) { // ���������� �������� ��������� ������������ ����� ������ � ������ ����. // �������� ����� ������ ���� ����� ��, ��� ID �������. ��� ����������� ������������ ��������. // ���������� ������ ���� ����� ��, ��� ���������� � ��������� ������������ �����. // Determine the final image file name with full path.�������������������� // The file name must be the same as the ID of the object. This ensures the uniqueness of the name.�������������������� // Extension must be the same as the expansion of the original image file. string strExtension = Path.GetExtension(imagefile.FileName); string strSaveFileName = objID + strExtension; string strSaveFullPath = Path.Combine(Server.MapPath(Url.Content("~/Content")), Resources.GlobalString.CATEGORY_MINI_IMAGES_FOLDER, strSaveFileName); // ���� ���� � ����� ��������� �������, ������� ���. //If a file by that name exists, delete it. if (System.IO.File.Exists(strSaveFullPath)) System.IO.File.Delete(strSaveFullPath); // ��������� ��������, ������� � �������. //Save the image by changing its size. imagefile.ResizeAndSave(Constants.CATEGORY_MINI_IMAGE_HEIGHT, Constants.CATEGORY_MINI_IMAGE_WIDTH, strSaveFullPath); // ���������� ����� ���������� � ���� ������ � ���� ImageExt. //The file extension is written to the database in the ImageExt. obj.ImageExt = strExtension; categoryRepository.Save(); } } catch (Exception ex) { string strErrorMessage = ex.Message; if (ex.InnerException != null) strErrorMessage = string.Format("{0} --- {1}", strErrorMessage, ex.InnerException.Message); ViewBag.ErrorMessage = strErrorMessage; return RedirectToAction("ErrorPage", "Home"); } return RedirectToAction("Details", new { id=obj.ID}); }
public ActionResult UploadCatalogImage(HttpPostedFileBase imagefile, int objID) { var obj = subcategoryRepository.Find(objID); if (obj == null) return View("NotFound"); try { if (imagefile != null) { string strExtension = Path.GetExtension(imagefile.FileName); string strSaveFileName = objID + strExtension; string strSaveFullPath = Path.Combine(Server.MapPath(Url.Content("~/Content")), Resources.GlobalString.SUBCATEGORY_MINI_IMAGES_FOLDER, strSaveFileName); if (System.IO.File.Exists(strSaveFullPath)) System.IO.File.Delete(strSaveFullPath); imagefile.ResizeAndSave(Constants.SUBCATEGORY_MINI_IMAGE_HEIGHT, Constants.SUBCATEGORY_MINI_IMAGE_WIDTH, strSaveFullPath); obj.ImageExt = strExtension; subcategoryRepository.Save(); } } catch (Exception ex) { string strErrorMessage = ex.Message; if (ex.InnerException != null) strErrorMessage = string.Format("{0} --- {1}", strErrorMessage, ex.InnerException.Message); ViewBag.ErrorMessage = strErrorMessage; return RedirectToAction("ErrorPage", "Home"); } return RedirectToAction("Details", new { id = obj.ID }); }