Open file in the browser from UI

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.

Who is Participating?
Alan WarrenConnect With a Mentor Applications DeveloperCommented:
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)
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
Michel PlungjanIT ExpertCommented:
Security risk

@Model.Link = "file:///C:\Data\allmypasswords.dat"
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

as mplungjan already explained: not possible in modern browsers due to SOP - Same Origin Policy
Alan WarrenApplications DeveloperCommented:
Response.Write("Understood! Thanks for the tip")

Open in new window


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.


> 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 ;-)
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.
>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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.