我有这样的链接:C:\ folder \ folder \ image.jpg。如何在SQL Server中将图像保存到数据库?
如果它是HttpPostedFileBase,我会知道如何保存它,但现在我只有一个链接。
谢谢。
答案 0 :(得分:1)
答案 1 :(得分: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();
}
答案 2 :(得分:0)
来自msdn social的例子:
表:
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