2012-09-17 96 views
0

我有一个这样的链接:C:\ folder \ folder \ image.jpg。如何将图像保存到SQL Server中的数据库?将图像保存到数据库MVC3

如果它是一个HttpPostedFileBase我会知道如何保存它,但现在我只有一个链接。

  • 这是一个链接,因为我从tiniMCE编辑器字段中取出了src标签。

谢谢。

回答

0

举例社会:

表:

CREATE TABLE [dbo].[FileStoreDemo](

       [id] [int] NOT NULL, 

       [name] [nvarchar](100) NOT NULL, 

       [content] [varbinary](max) NULL, 

       CONSTRAINT [pk_filestoredemo_id] PRIMARY KEY CLUSTERED ([id] ASC)) 

代码:

string filename = Server.MapPath("/Content/Desert.jpg") 
using (fooEntities fe = new fooEntities()) 

{ 

    FileStoreDemo fsd = fe.FileStoreDemoes.CreateObject(); 



    fsd.name = new FileInfo(filename).Name; 

    fsd.content = File.ReadAllBytes(filename); 

    fe.FileStoreDemoes.AddObject(fsd); 



    fe.SaveChanges(); 

} 

http://social.msdn.microsoft.com/Forums/en/sqlgetstarted/thread/1857938d-b74a-4954-bbde-734dfef08039

0
[HttpPost] 
     public ActionResult Create(string fileTitle) 
     { 
      try 
      { 
       HttpPostedFileBase file = Request.Files[0]; 
       byte[] imageSize = new byte[file.ContentLength]; 
       file.InputStream.Read(imageSize, 0, (int)file.ContentLength); 
       Image image = new Image() 
       { 
        Name = file.FileName.Split('\\').Last(), 
        Size = file.ContentLength, 
        Title = fileTitle, 
        ID = 1, 
        Image1 = imageSize 
       }; 
       db.Images.AddObject(image); 
       db.SaveChanges(); 
       return RedirectToAction("Detail"); 
      } 
      catch(Exception e) 
      { 
       ModelState.AddModelError("uploadError", e); 
      } 
      return View(); 
     } 
+0

谢谢。我现在就试一试。 –

+0

请尝试一下,让我知道一旦完成 – Pushpendra

+0

我得到这个错误:1. http://imageshack.us/a/img836/1708/errorsyv.png后面的代码是这样的:2. http:// imageshack .us/a/img38/4279/code1.png –