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) {
                alert("link:"+link);
                window.open(link);
        }
        </script>

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

or

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

Thanks.
thearthaAsked:
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: http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/ 

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)

            myConnection.Open()

            Dim myReader As System.Data.OleDb.OleDbDataReader = myCommand.ExecuteReader
            If myReader.Read Then
                Response.ContentType = myReader("MIMEType").ToString()
                Response.BinaryWrite(myReader("Data"))
            End If
            myReader.Close()
            myConnection.Close()
        End Using

Open in new window


hth

Alan ";0)
0
 
Friman001Commented:
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!

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

Ryan F
0
 
Michel PlungjanIT ExpertCommented:
Security risk
Imagine:

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

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

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

Open in new window

0
 
RoonaanCommented:
Hi,

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.

Regards,

-r-
0
 
ahoffmannCommented:
> Is this actually  a SOP issue?
yes
however, if you load the script (as posted in the question) from the local filesystem, then SOP is not violated, obviously ;-)
0
 
gureniusCommented:
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.
0
 
RoonaanCommented:
>yes
>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.
0
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.