Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I could not able to open pdf file from c drive. I tried below coding


What I have tried:

Dim lsfilename As String
Dim lsfilepath As String
hdnfilepath.Value = e.CommandArgument
Dim embed As String = "<object data=""{0}"" type=""application/pdf"" width=""500px"" height=""600px"">"
embed += "</object>"

objdbconn.OpenConn()
msSQL = "select document_gid,file_path, file_name from dap_trn_tdocuments where document_gid='" & hdnfilepath.Value & "'"
objOdbcDataReader = objdbconn.GetDataReader(msSQL)
If objOdbcDataReader.HasRows = True Then
objOdbcDataReader.Read()
lsfilename = objOdbcDataReader.Item("file_name").ToString
lsfilepath = objOdbcDataReader.Item("file_path").ToString
End If
'ltEmbed.Text = "<object data=""C:" & lsfilepath & """ type=""application/pdf"" width=""500px"" height=""600px""></object>"
'msSQL = " select * from tblfiles where id='" & id & "' "
ltEmbed.Text = String.Format(embed, ResolveUrl("C:" & lsfilepath & ""))
objdbconn.CloseConn()

My path is <object data="C:/erp_documents/local/daily process/uploaddocuments/2016/6/UPLF1606230135.pdf" type="application/pdf" width="500px" height="600px"></object>
Posted
Updated 27-Jun-16 20:45pm
Comments
Gokulprasad05 28-Jun-16 1:25am    
Process.Start("C:/erp_documents/local/daily process/uploaddocuments/2016/6/UPLF1606230135.pdf")

1 solution

That looks a lot like a website - so this won't work:
C#
Process.Start("C:/erp_documents/local/daily process/uploaddocuments/2016/6/UPLF1606230135.pdf")
It may appear to in development, but it won't in production.
C# code is always executed on the Server, not the Client, and only has access to Server disks, never the Client HDD. So depending on where you are storing the PDF it may not be possible at all.
Because the Process.Start code is executed on the Server, the process is started on the Server - so it attempts to use the system default file association to open the file and display it at the Server - the Client can't see it. It appears to work in development because they are the same machine, and you can't tell which started the process. But in production it fails.
The second problem is that even if the file is located on the server, your website may not have access to the folder containing it: IIS does not run under your user id, so it does not "inherit" your user access permissions. You need to check the folder chain and ensure that the correct privileges have been given.

You can display a PDF at the client (depending on the client and which browser he uses, some may treat it as a download rather than a page) by using the Response object:
C#
Response.ClearHeaders();
Response.ContentType = "application/pdf"
Response.Clear()
Response.AppendHeader("Content-Disposition", "inline")
Response.TransmitFile(pathToFile)
Response.End()
 
Share this answer
 
Comments
Gokulprasad05 28-Jun-16 3:29am    
Sir thanks for replying. My need is only i want to view the pdf not to download.
OriginalGriff 28-Jun-16 3:33am    
The client can't view the file until it's on his computer: otherwise malicious sites could force you to open anything they wanted on their servers...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900