在ASP.NET页面中显示数据库中的PDF或DOC

时间:2014-09-18 20:25:19

标签: asp.net pdf

您好我想将PDF文件或.DOC或保存在我的数据库中的图像显示在我的ASP.net页面中。 我尝试了这个代码,但没有成功。有什么想法我该怎么办?

  <object type="application/pdf" data="~/Protected/docs/CV_Zied_JOUINI.pdf" width="400"     height="300">
<param name="movie" value="~/Protected/docs/CV_Zied_JOUINI.pdf" />
<img src="~/Protected/docs/CV_Zied_JOUINI.pdf" alt="" width="200" height="100" />

4 个答案:

答案 0 :(得分:1)

  

我想显示PDF文件或.DOC或图像保存在我的数据库中   进入我的ASP.net页面

如果文件保存在数据库中,则通常采用二进制格式。

如果是这样,您需要文件处理程序才能将这些二进制数据显示回客户端 浏览器。

例如,

public class FileHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string id = context.Request.QueryString["id"];

        // Let say you get the file data from database based on id
        // ...
        var fileData = new byte[] { ... };

        string fileName = "PDF_FILENAME.pdf";
        context.Response.Clear();
        // Need to return appropriate ContentType for different type of file.
        context.Response.ContentType = "application/pdf";
        context.Response.AddHeader("Content-Disposition", 
           "attachment; filename=" + fileName);
        context.Response.AddHeader("Content-Length", fileData.Length.ToString());
        context.Response.Write(fileData);
        context.Response.End();
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

用法

<a href="/FileHandler.ashx?id=1">My File</a>

答案 1 :(得分:0)

如果没有看到您的代码,您可以考虑将object标记放在div内。我不相信您需要PDF的paramimg标签。试试这个,

<div> <object data="~/Protected/docs/CV_Zied_JOUINI.pdf" type="application/pdf" width="300" height="200"> alt : <a href="test.pdf">test.pdf</a> </object> </div>

答案 2 :(得分:0)

假设您的意思是关系数据库而不仅仅是文件系统(您的问题不明确)。

  1. 创建Generic Handler(.ashx)。此处理程序应接受查询字符串参数,该参数指定要检索的文件的标识。这可能是文件名或ID,具体取决于您配置数据库的方式。
  2. 通用处理程序应该将数据库中的文件作为字节数组以及任何元数据(如内容类型)检索。
  3. 通用处理程序应使用Response.ContentType为其返回的文件将其响应标头设置为适当的内容类型,并使用Response.BinaryWrite()将文件写入响应。
  4. HTML中<object><img>元素中的路径应指向通用处理程序,确保使用查询字符串参数传递所请求文件的标识,例如:{{1其中fileretriever.ashx?document=XXX-XXX是文件的标识。

答案 3 :(得分:0)

谢谢大家。我使用了这段代码和它的工作

<iframe src="/Protected/docs/CV_Zied_JOUINI.pdf" width="1320px" height="1500px"></iframe>
谢谢你:)