locked
Showing a PDF file based on info from database RRS feed

  • Question

  • User2021693309 posted

    Hello, I have a small Access database with some fields, like: date, name, text. Those fields are stored in a record inside MS Access.
    I also have an PDF file available belonging to that record as described before. I like to store the title of this PDF also in this record.
    Clicking by an user on this title should show the PDF file in a separate form.

    The pdf's are all stored in a map called e:\pdfs\ and filename can be as needed.

    My questions are:
    1. how should i bring the pdf names inside the record (like e:\pdfs\number-one.pdf ? )
    2. by clicking on this name the pdf file should be showed in a seperate form?
    3. clicking means probably a javascript that handles the pdf file ?
    4. can anyone give me some clues?
    I do work with VS2010 and framework 4
    Thanks for your answers

    Thursday, August 5, 2010 1:12 PM

Answers

  • User1508394307 posted

    Regarding your questions

    1. Makes sense to store only a file name. To get the full path you can use "e:\pdfs\" + FileName. It would help if you'll decide later on to move PDFs to another server/folder, in this case you will only need to change "e:\pdfs" without updating all records in the database. It would also save space on the database. A directory name can be also configured in the web.config file.
    2. It's up to you. I usually do it in a separate form because I prefer to keep the original page opened.
    3. It can be also done with a simple HyperLink Control where you would need to specify the path to the PDF (MyHyperLink.NavigateUrl=FileName). To open PDF in a new window use Target="_blank". To open it from the code behind using a javascript you can do something like this

    protected void Button1_Click(object sender, EventArgs e)
    {
        string FileName = ...;
        
        Type cstype = this.GetType();
        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;
        cs.RegisterClientScriptBlock(cstype, "", "window.open('" + FileName + "');", true);
    }

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 6, 2010 3:43 AM

All replies

  • User1508394307 posted

    Regarding your questions

    1. Makes sense to store only a file name. To get the full path you can use "e:\pdfs\" + FileName. It would help if you'll decide later on to move PDFs to another server/folder, in this case you will only need to change "e:\pdfs" without updating all records in the database. It would also save space on the database. A directory name can be also configured in the web.config file.
    2. It's up to you. I usually do it in a separate form because I prefer to keep the original page opened.
    3. It can be also done with a simple HyperLink Control where you would need to specify the path to the PDF (MyHyperLink.NavigateUrl=FileName). To open PDF in a new window use Target="_blank". To open it from the code behind using a javascript you can do something like this

    protected void Button1_Click(object sender, EventArgs e)
    {
        string FileName = ...;
        
        Type cstype = this.GetType();
        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;
        cs.RegisterClientScriptBlock(cstype, "", "window.open('" + FileName + "');", true);
    }

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 6, 2010 3:43 AM
  • User-1199946673 posted

    by clicking on this name the pdf file should be showed in a seperate form?
     

    When you want to show (pdf) files, they should be stored inside the webroot. That means that when someone knows the direct link to the pdf, the pdf is downloaded unless you do something in IIS! with IIS7, you could also do that in web.config

    If you don't want that, you could store the file outside the webroot (or in the App_Data folder of the webroot, which wiil prevent users to directly accessing the file). In that case, you need a file handler to show the file. All is explained here:

    http://www.mikesdotnetting.com/Article/122/Simple-File-Download-Protection-with-ASP.NET

     

    Friday, August 6, 2010 3:56 PM
  • User2021693309 posted

    Thanks for the Tip Hans. I will do that Smile 

    Friday, August 6, 2010 4:05 PM