2012-05-11 2 views
1

업로드 컨트롤로 업로드 한 다음 해당 이미지를 축소판으로 변환하고 이미지 폴더의 실제 이미지와 축소판 이미지 모두를 asp.net의 축소판 이미지 폴더에 저장하십시오. -mvc3해당 이미지를 축소판으로 변환하고 두 이미지를 asp.net-mvc3

+0

크기 조정을 위해 타사 라이브러리를 사용할 수 있습니까? 이미지 크기를 조정할 때 코드를 사용하지만 타사 라이브러리를 사용합니다. –

답변

1

Image Resizer이라는 타사 라이브러리를 사용합니다. 나는 이미지를 업로드하고 품질을 유지하는 데 어려움을 겪었고,이 라이브러리는 나를 많이 도와 주었다. 보기에

:

@model YourProject.ViewModels.ProductImageUploadCreateViewModel 
@using (Html.BeginForm("Upload", "ProductImage", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="ImageFile1" id="ImageFile1"> 
} 

컨트롤러 :

public class ProductImageController : Controller 
{ 
    [HttpPost] 
    public ActionResult Upload(ProductImageUploadCreateViewModel viewModel) 
    { 
      if (!ModelState.IsValid) 
      { 
       return View(viewModel); 
      } 

      if (viewModel.ImageFile1 != null) 
      { 
       UploadImage(viewModel.ProductId, "1", viewModel.ImageFile1); 
      } 

      // Return to where ever... 
    } 
} 

업로드 방법, 샘플 몇

private void UploadImage(int productId, string imageNo, HttpPostedFileBase imageFile) 
{ 
    string uploadPath = Server.MapPath("~/Assets/Images/Products/" + productId); 
    if (!Directory.Exists(uploadPath)) 
    { 
      Directory.CreateDirectory(uploadPath); 
    } 

    Dictionary<string, string> versions = new Dictionary<string, string>(); 
    versions.Add("_m", "width=150&height=150&scale=both&format=jpg"); // Medium size 

    string filePrefix = productId + "_" + imageNo; 
    versions.Add("_s", "width=90&height=90&scale=both&format=jpg"); // Small size 
    versions.Add("_l", "width=300&height=300&scale=both&format=jpg"); // Large size 

    foreach (string fileSuffix in versions.Keys) 
    { 
      // Generate a filename 
      string fileName = Path.Combine(uploadPath, filePrefix + fileSuffix); 

      // Let the image builder add the correct extension based on the output file type 
      fileName = ImageBuilder.Current.Build(imageFile, fileName, new ResizeSettings(versions[fileSuffix]), false, true); 
    } 
} 

자신의 웹 사이트에 보라가 그 너는 잘 할 수있어. 이 도움이되기를 바랍니다 :)

관련 문제