?
Solved

Open file in the browser from UI

Posted on 2012-08-15
10
Medium Priority
?
397 Views
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) {
                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.
0
Comment
Question by:theartha
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 4

Expert Comment

by:Friman001
ID: 38299172
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38299202
Security risk
Imagine:

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

Accepted Solution

by:
Alan Warren earned 1500 total points
ID: 38299274
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Expert Comment

by:ahoffmann
ID: 38299456
as mplungjan already explained: not possible in modern browsers due to SOP - Same Origin Policy
0
 
LVL 26

Expert Comment

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

Open in new window

0
 
LVL 49

Expert Comment

by:Roonaan
ID: 38299840
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 38299867
> 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
 
LVL 1

Expert Comment

by:gurenius
ID: 38300708
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
 
LVL 49

Expert Comment

by:Roonaan
ID: 38300714
>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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

749 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