2

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

ben rudgers
  • 3,647
  • 2
  • 20
  • 32
user3773380
  • 145
  • 1
  • 2
  • 12
  • The path to your files isn't correct. The `~/` is virtual path syntax only intended for use on the server side. You'll need to use something like described in [this question](http://stackoverflow.com/questions/353165/converting-virtual-path-to-actual-web-path-in-asp-net) to convert it to a path usable by the client. – mason Sep 18 '14 at 20:28

4 Answers4

1

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>
Win
  • 61,100
  • 13
  • 102
  • 181
  • thank you for your help. i will explain my question. im making a website using ASP.NET and i made a directory called docs (/Protected/docs/) inside my web project. I gave the possibility to user to upload file, then i want to give him the possibility to view this uploded file inside another page before downloading it. – user3773380 Sep 19 '14 at 15:54
0

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>
killQuotes
  • 178
  • 13
  • 1
    As I pointed out in my comment on the question, you can't use the `~/` syntax on the client side. – mason Sep 18 '14 at 20:33
  • @mason, Right, I used the code the OP had for the 'data' attribute. Sorry about that. – killQuotes Sep 18 '14 at 20:40
  • @mason thank you for your help. i put the code inside
    and i changed the link to a file with absolute path but is the same thing. it just displays a grau square.
    – user3773380 Sep 19 '14 at 15:47
  • @user3773380 Is the path to the file correct? If you manually navigate there, do you see the file? Have you tried implementing either a file handler or generic handler as Win and my answers described? – mason Sep 19 '14 at 15:51
0

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.
Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
0

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

user3773380
  • 145
  • 1
  • 2
  • 12