Open file in the browser from UI

Posted on 2012-08-15
Last Modified: 2012-09-27
I have a file on my local drive C:\Data\file.pdf.

I am trying to open the file from UI using

@Model.Link = "file:///C:\Data\file.pdf"

 <script language="javascript" type="text/javascript">
            function openFile(link) {

<input type="button" id="" value="PDF" onclick="openFile('@Model.Link');"/>


<input type="button" id="" value="PDF" onclick="window.location.href='@Model.Link'"/>

both the tags were unable to open the PDF in the browser.

Please advice.

Question by:theartha
    LVL 4

    Expert Comment

    You would be better off using a FileUpload control if you are using ASP.NET.  This control would allow you to find a file and then save it using C# code or maybe even JavaScript!

    Ryan F
    LVL 75

    Expert Comment

    by:Michel Plungjan
    Security risk

    @Model.Link = "file:///C:\Data\allmypasswords.dat"
    LVL 26

    Accepted Solution

    Hi theartha
    you have to get the file to server, ajaxToolkit:AsyncFileUpload is my preference:

    Then you can  open the uploaded pdf in a new window or an iframe, you could even store the uploaded pdf bytes in a db then extract it using a surrogate page that has the response.contenttype set to 'MimeType'.

    Here's an example of vb to suck the raw binary from an access 2007 db catalog and write it to a page.

           'Connect to the database and bring back the pdf contents & MIME type for the specified pdf file
            Using myConnection As New System.Data.OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ACE.OLEDB.12.0_PDF").ConnectionString) ' Defined in web.config
                Const SQL As String = "SELECT * FROM [tblPDF] WHERE [ID] = @ID"
                Dim myCommand As New System.Data.OleDb.OleDbCommand(SQL, myConnection)
                ' Dim myCommand As New SqlCommand(SQL, myConnection)
                myCommand.Parameters.AddWithValue("@ID", ID)
                Dim myReader As System.Data.OleDb.OleDbDataReader = myCommand.ExecuteReader
                If myReader.Read Then
                    Response.ContentType = myReader("MIMEType").ToString()
                End If
            End Using

    Open in new window


    Alan ";0)
    LVL 51

    Expert Comment

    as mplungjan already explained: not possible in modern browsers due to SOP - Same Origin Policy
    LVL 26

    Expert Comment

    by:Alan Warren
    Response.Write("Understood! Thanks for the tip")

    Open in new window

    LVL 49

    Expert Comment


    Is this actually  a SOP issue?

    The question is regarding the opening of a PDF in the browser. This is something you cannot really control from html or javascript. There are no magic javascript statements, or HTML attributes that control this behavior. There are some HTTP Headers that increase the likelyhood of a PDF opening inside your browser, but there still is no guarantee it will on every machine/os/browser, nor will there ever be as far as I know.


    LVL 51

    Expert Comment

    > Is this actually  a SOP issue?
    however, if you load the script (as posted in the question) from the local filesystem, then SOP is not violated, obviously ;-)
    LVL 1

    Expert Comment

    You are trying to open a local file. All answers about uploading isn't relevant for that.

    How about a standard link?

    <a href="file:///C:\Data\file.pdf" target="_blank">My PDF</a>

    Open in new window

    Depending on what browser you are using, you may get a warning before the PDF opens.
    LVL 49

    Expert Comment

    >however, if you load the script (as posted in the question) from the local filesystem,
    > then SOP is not violated, obviously ;-)

    The question doesn't state the PDF isn't loaded, or that a warning is shown, which would be the case when a SOP violation would have happened. The question states that the PDF is not loaded in the browser.

    Even with the right SOP in place, you still don't have control over the PDF loading in the browser window or not.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
    Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
    In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
    In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now