Solved

There was an error opening this document.  This file cannot be found.

Posted on 2006-07-15
7
4,006 Views
Last Modified: 2008-03-10
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("Content-Disposition", "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(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)

                        dataToRead = iStream.Length
                        'Response.AddHeader("Content-Type", "application/pdf")
                        Response.AddHeader("Content-Length", Fi.Length.ToString())
                        Response.AddHeader("Content-Disposition", "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.Write(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("Commonerror.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
0
Comment
Question by:vkarumbaiah
  • 4
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17114415
1) Try using Server.MapPath("Myfile.pdf")

2) Do you have a virtual directory set up?

bob
0
 
LVL 1

Author Comment

by:vkarumbaiah
ID: 17114475
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17114872
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:vkarumbaiah
ID: 17115418

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/mypdf.pdf")

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


0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17115674
Here is some good advice:

http://www.velocityreviews.com/forums/t110933-reading-and-writing-to-a-virtual-directory.html

1) Use Server.MapPath to get the physical directory name.

2) That's because the asp.net account needs permisions to the network share, and that has nothing to do with the permissions for the local file system.

3) Make sure the network share allows access to the server's asp.net account.

Bob
0
 
LVL 1

Author Comment

by:vkarumbaiah
ID: 17217792
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(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)

                        dataToRead = iStream.Length

Response.ClearContent()
Response.ClearHeaders()

                           Response.AppendHeader("Content-Type", "application/pdf")
                           Response.AppendHeader("Content-Length", Fi.Length.ToString()) ' This header informs the client browser of the file length(size) in bytes
                           Response.AppendHeader("Content-Disposition", "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.Write(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("Commonerror.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

0
 
LVL 1

Author Comment

by:vkarumbaiah
ID: 17245019
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
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert html to string 6 38
EF5: Question about Metadata Artifact Processing 4 21
C# LINQ ForEach() question 6 53
asp Google Map 2 24
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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