我有一个这样的链接:C:\ folder \ folder \ image.jpg。如何将图像保存到SQL Server中的数据库?将图像保存到数据库MVC3
如果它是一个HttpPostedFileBase我会知道如何保存它,但现在我只有一个链接。
- 这是一个链接,因为我从tiniMCE编辑器字段中取出了src标签。
谢谢。
我有一个这样的链接:C:\ folder \ folder \ image.jpg。如何将图像保存到SQL Server中的数据库?将图像保存到数据库MVC3
如果它是一个HttpPostedFileBase我会知道如何保存它,但现在我只有一个链接。
谢谢。
举例社会:
表:
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
[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();
}
谢谢。我现在就试一试。 –
请尝试一下,让我知道一旦完成 – Pushpendra
我得到这个错误:1. http://imageshack.us/a/img836/1708/errorsyv.png后面的代码是这样的:2. http:// imageshack .us/a/img38/4279/code1.png –
谢谢。我会先尝试Pushpendra的回答,然后如果它不起作用,我会阅读你的链接。 –