2011-02-13 74 views
1

你好我會想實現我的應用程序的代碼,但我不知道如何將圖像名稱添加到數據庫中,當一個文件被上傳MVC上傳圖像與存儲在數據庫文件名

sing System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using accomm2.Models; 
using System.IO; 
using System.Drawing; 
using System.Drawing.Drawing2D; 

namespace accomm2.Controllers 
{ 
    public class AdminController : Controller 
    { 
     dataEntities3 _db = new dataEntities3(); 

     //fileupload 

     public ActionResult UploadImage() 
     { 
      ViewBag.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(); 
      ViewBag.listImages = listImages; 

      return View("UploadImage"); 
     } 



     [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); 

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


        } 
       } 

      } 
      return RedirectToAction("UploadImage"); 
     } 

     public void ResizeImage(string filepath, int imageSize) 
     { 
      var newImage = new Bitmap(Server.MapPath(filepath)); 
      int thumbnailSize = imageSize; 
      int newWidth, newHeight; 

      if (newImage.Width > newImage.Height) 
      { 
       newWidth = thumbnailSize; 
       newHeight = newImage.Height * thumbnailSize/newImage.Width; 
      } 
      else 
      { 
       newWidth = newImage.Width * thumbnailSize/newImage.Height; 
       newHeight = thumbnailSize; 
      } 

      var thumbnailBitmap = new Bitmap(newWidth, newHeight); 

      var thumbnailGraph = Graphics.FromImage(thumbnailBitmap); 
      thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality; 
      thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality; 
      thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic; 

      var imageRectangle = new Rectangle(0, 0, newWidth, newHeight); 
      thumbnailGraph.DrawImage(newImage, imageRectangle); 

      filepath = filepath.Replace("/StoredImages/Images/", "/StoredImages/Thumbs/"); 
      thumbnailBitmap.Save(Server.MapPath(filepath)); 
      thumbnailGraph.Dispose(); 
      thumbnailBitmap.Dispose(); 
      newImage.Dispose(); 
     } 

} 
} 

我的實際DB:

model

謝謝

+1

您應該閱讀一些關於實體框架的教程。您的問題與上傳圖片無關,但您不知道如何在EF中執行基本操作。 – LukLed 2011-02-13 14:37:09

+0

謝謝..我知道是像_db.PropPicture.AddObject(道具); _db.SaveChanges();但我不知道如何整合這個上傳..我敢肯定有人對這個答案感興趣,謝謝 – Teodor 2011-02-13 16:01:40

回答

2

只需添加專線爲您保存圖像後立即對數據庫插入記錄。代碼應該是這樣的:

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); 

         /**** begin db saving ****/ 
         PropPicture prop = new PropPicture(); 
         prop.PictureName = imageName; 
         _db.PropPicture.AddObject(prop); 
         _db.SaveChanges(); 
         /**** end db saving ****/ 

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


        } 
       } 

只需再次檢查「數據庫保存」的代碼。我在這臺電腦上沒有Visual Studio,所以我無法測試它是否正確。 :)