This is a mirror of official site: http://jasper-net.blogspot.com/

Example Upload & Resize Images in ASP.NET MVC

| Tuesday, September 14, 2010
Today I write Controller used Upload & Resize Images in ASP.NET MVC

Step One : I create Controller  used to Upload and display photos uploaded list

Here this code :
public ActionResult UploadImage()
       {
           ViewData["Message"] = "Welcome to GeeksShip.com! Example Upload & Resize Images";
           const string folderThumbPath = "/Content/StoredImages/Thumbs/";

           var directoryThumbs = new DirectoryInfo(Server.MapPath(folderThumbPath));

           var listImages = directoryThumbs.GetFiles().Select(file => file.Name).ToList();
           ViewData["listImages"] = listImages;
           return View("/Views/Home/UploadImage.aspx");
       }

Step Two: I create Controller used to save image upload and resize it
Here this code:

[AcceptVerbs(HttpVerbs.Post)]
       public ActionResult UploadImage(string str)
       {
           if (ModelState.IsValid)
           {
               if (Request.Files != null)
               {
                   var posted = Request.Files["uploadFile"];
                   if (posted.FileName != "")
                   {


                       const string pathStoredImage = "/Content/StoredImages/Images/";
                       const string pathStoredThumbs = "/Content/StoredImages/Thumbs/";


                       var imageName = Path.GetFileName(posted.FileName);
                       var filePath = pathStoredImage + imageName;

                       posted.SaveAs(Server.MapPath(filePath));
                       ResizeImage(filePath, 150);

                       ViewData["message"] = ViewData["message"] + "<br >" +
                       "Đã upload <b>" + posted.FileName +"</b> hình ảnh thành công!";

                   }
               }

           }
           return RedirectToAction("UploadImage");
       }

Read more: Geeks Ship

Posted via email from .NET Info

0 comments: