2011-02-13 72 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,所以我无法测试它是否正确。 :)