user3773380
user3773380

Reputation: 145

Display a PDF or DOC from database in an ASP.NET page

hi i want to display a PDF file or .DOC or an image saved in my Data Base into my ASP.net page. i tryed this code but without sucess. have any one an idea how can i do it ?

  <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" />

Upvotes: 2

Views: 5187

Answers (4)

user3773380
user3773380

Reputation: 145

Thank you for all. I used this code and it's work

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

thank you :)

Upvotes: 0

mason
mason

Reputation: 32694

Assuming you meant a relational database instead of just the file system (your question was ambiguous).

  1. Create a Generic Handler (.ashx). This handler should accept a query string parameter specifying what the identity of the file to retrieve is. This might be a filename or ID, it depends on how you've configured your database.
  2. The generic handler should retrieve the file from the database as a byte array, along with any meta data such as content type.
  3. The generic handler should set its response headers to the appropriate content type using Response.ContentType for the file it is returning, and write the file to the response using Response.BinaryWrite().
  4. The paths in your <object> or <img> elements in the HTML should point to the generic handler, making sure to pass the identity of the requested file with a query string parameter, ex: fileretriever.ashx?document=XXX-XXX where XXX-XXX is the identify of the file.

Upvotes: 0

Win
Win

Reputation: 62260

i want to display a PDF file or .DOC or an image saved in my Data Base into my ASP.net page

If a file is saved in Database, it is normally in Binary format.

If so, you need a File Handler in order to display those Binary Data back to client browser.

For example,

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

Usage

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

Upvotes: 1

killQuotes
killQuotes

Reputation: 178

Without seeing your code in action, you might consider placing the object tag inside a div. I'm not convinced you need the param and img tags for a PDF though. Try this,

<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>

Upvotes: 0

Related Questions