从数据库中提取PDF并将其显示在我的cshtml上 [英] Extract pdf from database and display it on my cshtml

查看:222
本文介绍了从数据库中提取PDF并将其显示在我的cshtml上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目上工作时,应该会显示resturant信息和pdf文件。我已经能够将我的pdf保存到我的数据库中。但我不知道如何从我的数据库中取出它,然后将它显示在我的.cshtml上。



我正在使用MS和MSSQL服务器来处理VS和MVC4。



下载文件的代码。

  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;组; } 

数据存储为

  [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页面下载。

解决方案

你可能需要的是一个动作输出你的PDF,例如:

  public ActionResult GetDocument(int id)
{
TempVM model = GetViewModel();如果(model.PDF_Data.Length> 0)

返回File(model.PDF_Data,model.PDF_ContentType);
}
else
{
return View(NotFound);




$ b $ p
$ b然后在你的输出视图中,作为一个超链接,就像这样:

  @ Html.ActionLink(View doc,GetDocument,new {ID = 1 })


i working on a project, that shall show resturant information along with an pdf file. I have been able to save my pdf to my database. But i don't know how to extrakt it from my database and then display it on my .cshtml.

I'm working with VS and MVC4 with an MSSQL server.

Heres the code to download the file.

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; 

The file comes from the object

public HttpPostedFileBase PDF_File { get; set; }

And the data is stored as

[PDF_Data]    VARBINARY (MAX) NULL,
[PDF_ContentType] VARCHAR(MAX) NULL, 
[PDF_FileName] VARCHAR(MAX) NULL,

With the sql code to store it

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;

But know i dont know how to extrakt my pdf file stored in my database and then display it on my .cshtml page so i can view it and mayby download it from the -cshtml page.

解决方案

What you probably need is an action to output your PDF, e.g.:

    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");
        }
    }

And then on your output view, you just include this as a hyperlink, like so:

@Html.ActionLink("View doc", "GetDocument", new {ID = 1})

这篇关于从数据库中提取PDF并将其显示在我的cshtml上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆