我正在开展一个项目,该项目将显示resturant信息以及pdf文件。我已经能够将我的pdf保存到我的数据库中。但我不知道如何从我的数据库中提取它,然后将其显示在我的.cshtml上。
我正在使用VS和MVC4和MSSQL服务器。
下载文件的代码。
var fileName = Path.GetFileName(TempVM.Resturang.PDF_File.FileName);
var filePath = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
TempVM.Resturang.PDF_File.SaveAs(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
TempVM.Resturang.PDF_FileName = fileName;
TempVM.Resturang.PDF_Data = bytes;
TempVM.Resturang.PDF_ContentType = filePath;
该文件来自对象
public HttpPostedFileBase PDF_File { get; set; }
数据存储为
[PDF_Data] VARBINARY (MAX) NULL,
[PDF_ContentType] VARCHAR(MAX) NULL,
[PDF_FileName] VARCHAR(MAX) NULL,
用sql代码存储它
cmd.Parameters.Add("@PDF_FileName", SqlDbType.VarChar).Value = Resturang.PDF_FileName;
cmd.Parameters.Add("@PDF_ContentType", SqlDbType.VarChar).Value = Resturang.PDF_ContentType;
cmd.Parameters.Add("@PDF_Data", SqlDbType.VarBinary).Value = Resturang.PDF_Data;
但是知道我不知道如何将我的pdf文件存储在我的数据库中,然后将其显示在我的.cshtml页面上,这样我就可以查看它并可以从-cshtml页面下载它。
答案 0 :(得分:1)
您可能需要的是输出PDF的操作,例如:
public ActionResult GetDocument(int id)
{
TempVM model = GetViewModel();
if (model.PDF_Data.Length > 0)
{
return File(model.PDF_Data, model.PDF_ContentType);
}
else
{
return View("NotFound");
}
}
然后在输出视图中,您只需将其包含为超链接,如下所示:
@Html.ActionLink("View doc", "GetDocument", new {ID = 1})