vkarumbaiah
asked on
There was an error opening this document. This file cannot be found.
Hi
Im using the following code to download a pdf. It works well if the user clicks the save button on the save file dialog to download the pdf to disk but doesnt if the user clicks the open button instead it appears to download the file to a temporary folder and then when it trys to open it in Acrobat I get the message "There was an error opening this document. This file cannot be found.".
In the following code if I comment out the header
Response.AddHeader("Conten t-Disposit ion", "attachment; filename=myfile.pdf")
everything works well except that the PDF file name is set to the name of the ASPX page name instead of Myfile.pdf
Is there a way retain the name of the pdf file if I comment out the "Content-Disposition" header if not how can I get it to open the file when the user click the open button on the save file dialog?
Dim Fi As FileInfo = New FileInfo("C:\Myfile.pdf")
Try
iStream = New System.IO.FileStream(filep ath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
dataToRead = iStream.Length
'Response.AddHeader("Conte nt-Type", "application/pdf")
Response.AddHeader("Conten t-Length", Fi.Length.ToString())
Response.AddHeader("Conten t-Disposit ion", "attachment; filename=myfile.pdf")
' Read the bytes.
While dataToRead > 0
' Verify that the client is connected.
If Response.IsClientConnected Then
' Read the data in buffer
length = iStream.Read(buffer, 0, 10000)
' Write the data to the current output stream.
Response.OutputStream.Writ e(buffer, 0, length)
' Flush the data to the HTML output.
Response.Flush()
ReDim buffer(10000) ' Clear the buffer
dataToRead = dataToRead - length
Else
' Prevent an infinite loop if user disconnects
dataToRead = -1
End If
End While
Catch ex As Exception
' Trap the error, if any
Response.Redirect("Commone rror.aspx? Err=Error : " & ex.Message, False)
Finally
If IsNothing(iStream) = False Then
' Close the file.
iStream.Close()
' Set FileInfo to nothing
Fi = Nothing
Session.Clear() ' Clear any keys and values form the session
Session.Abandon() ' Cancel the session
Response.Close() ' Close the connection to the client
Response.Clear() ' Clear the stream buffer
End If
End Try
Im using the following code to download a pdf. It works well if the user clicks the save button on the save file dialog to download the pdf to disk but doesnt if the user clicks the open button instead it appears to download the file to a temporary folder and then when it trys to open it in Acrobat I get the message "There was an error opening this document. This file cannot be found.".
In the following code if I comment out the header
Response.AddHeader("Conten
everything works well except that the PDF file name is set to the name of the ASPX page name instead of Myfile.pdf
Is there a way retain the name of the pdf file if I comment out the "Content-Disposition" header if not how can I get it to open the file when the user click the open button on the save file dialog?
Dim Fi As FileInfo = New FileInfo("C:\Myfile.pdf")
Try
iStream = New System.IO.FileStream(filep
dataToRead = iStream.Length
'Response.AddHeader("Conte
Response.AddHeader("Conten
Response.AddHeader("Conten
' Read the bytes.
While dataToRead > 0
' Verify that the client is connected.
If Response.IsClientConnected
' Read the data in buffer
length = iStream.Read(buffer, 0, 10000)
' Write the data to the current output stream.
Response.OutputStream.Writ
' Flush the data to the HTML output.
Response.Flush()
ReDim buffer(10000) ' Clear the buffer
dataToRead = dataToRead - length
Else
' Prevent an infinite loop if user disconnects
dataToRead = -1
End If
End While
Catch ex As Exception
' Trap the error, if any
Response.Redirect("Commone
Finally
If IsNothing(iStream) = False Then
' Close the file.
iStream.Close()
' Set FileInfo to nothing
Fi = Nothing
Session.Clear() ' Clear any keys and values form the session
Session.Abandon() ' Cancel the session
Response.Close() ' Close the connection to the client
Response.Clear() ' Clear the stream buffer
End If
End Try
ASKER
No I dont have a virtual directory set up since Im getting the file off of a network share so the path to the file is \\MachineName\MyFile.pdf
My apologies for not having mentioned that earlier
My apologies for not having mentioned that earlier
If you have anonymous access to web site configured, it is a security issue to get files on any directory on the computer, so you need to grant specific right for that to happen. And, you want to grant specific rights, and not make broad sweeping changes to allow access.
Bob
Bob
ASKER
I have set a virtual directoy set up now on IIS and its target directory is "a share located on another computer"
filepath = Server.MapPath("/pdfs/mypd
However I have the same problemr that is when I click the open button I get the error "There was an error opening this document. This file cannot be found."
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi bob
I was able to solve the problem by clearing out the headers first before setting them i.e as below
The rest of the code remained the same as above. This now works reliably across browsers and platforms.
The website runs under the context of a domain user account that has very specific privilages and has read access to the network share.
Thnak you for your help the link you sent me helped a great deal in understanding the concepts.
Try
iStream = New System.IO.FileStream(filep ath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
dataToRead = iStream.Length
Response.ClearContent()
Response.ClearHeaders()
Response.AppendHeader("Con tent-Type" , "application/pdf")
Response.AppendHeader("Con tent-Lengt h", Fi.Length.ToString()) ' This header informs the client browser of the file length(size) in bytes
Response.AppendHeader("Con tent-Dispo sition", "attachment; filename=myfile.pdf;") 'This is required in order to show the file download dialog, ommiting this will directly open the pdf on the client
' Read the bytes.
While dataToRead > 0
' Verify that the client is connected.
If Response.IsClientConnected Then
' Read the data in buffer
length = iStream.Read(buffer, 0, 10000)
' Write the data to the current output stream.
Response.OutputStream.Writ e(buffer, 0, length)
' Flush the data to the HTML output.
Response.Flush()
ReDim buffer(10000) ' Clear the buffer
dataToRead = dataToRead - length
Else
' Prevent an infinite loop if user disconnects
dataToRead = -1
End If
End While
Catch ex As Exception
' Trap the error, if any
Response.Redirect("Commone rror.aspx? Err=Error : " & ex.Message, False)
Finally
If IsNothing(iStream) = False Then
' Close the file.
iStream.Close()
' Set FileInfo to nothing
Fi = Nothing
Session.Clear() ' Clear any keys and values form the session
Session.Abandon() ' Cancel the session
Response.Close() ' Close the connection to the client
Response.Clear() ' Clear the stream buffer
End If
End Try
I was able to solve the problem by clearing out the headers first before setting them i.e as below
The rest of the code remained the same as above. This now works reliably across browsers and platforms.
The website runs under the context of a domain user account that has very specific privilages and has read access to the network share.
Thnak you for your help the link you sent me helped a great deal in understanding the concepts.
Try
iStream = New System.IO.FileStream(filep
dataToRead = iStream.Length
Response.ClearContent()
Response.ClearHeaders()
Response.AppendHeader("Con
Response.AppendHeader("Con
Response.AppendHeader("Con
' Read the bytes.
While dataToRead > 0
' Verify that the client is connected.
If Response.IsClientConnected
' Read the data in buffer
length = iStream.Read(buffer, 0, 10000)
' Write the data to the current output stream.
Response.OutputStream.Writ
' Flush the data to the HTML output.
Response.Flush()
ReDim buffer(10000) ' Clear the buffer
dataToRead = dataToRead - length
Else
' Prevent an infinite loop if user disconnects
dataToRead = -1
End If
End While
Catch ex As Exception
' Trap the error, if any
Response.Redirect("Commone
Finally
If IsNothing(iStream) = False Then
' Close the file.
iStream.Close()
' Set FileInfo to nothing
Fi = Nothing
Session.Clear() ' Clear any keys and values form the session
Session.Abandon() ' Cancel the session
Response.Close() ' Close the connection to the client
Response.Clear() ' Clear the stream buffer
End If
End Try
ASKER
Here are some other links to information I found related to this problem
http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/#S2
http://www.devx.com/dotnet/Article/22533
http://msdn.microsoft.com/msdnmag/issues/06/09/WebDownloads/#S2
http://www.devx.com/dotnet/Article/22533
2) Do you have a virtual directory set up?
bob