Srinivas
Srinivas

Reputation: 171

Page execution / lifecycle stops after Response.TransmitFile

I have a gridview and a download button for each and every row. the page objective is to list out all the records related to physical files, and when download button clicked it should download and open the downloaded file. the files are in 'pdf' format.

The page is coded and everything is going fine. I want to rebind the gridview when the file got downloaded. but after download, the rest of the page's execution or lifecycle is not being done.

it seems the issue is with page handlers. I tried searching over the internet, but unfortunately no solution found. I request you experts please help me in this issue. and my code is like below.

Dim outputStream As FileStream = New FileStream((Server.MapPath("../ScannedDoc") + ("\" + fileName)), FileMode.Create) reqFTP = CType(FtpWebRequest.Create(New Uri(DataDirectory + "InternalDocuments/" + folderName + "/" + fileName)), FtpWebRequest)

                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
                reqFTP.UseBinary = True
                reqFTP.Credentials = New NetworkCredential(uid, pwd)
                Dim ftpresponse As FtpWebResponse = CType(reqFTP.GetResponse, FtpWebResponse)

                Dim ftpStream As Stream = ftpresponse.GetResponseStream
                Dim cl As Long = ftpresponse.ContentLength
                Dim bufferSize As Integer = 2048
                Dim readCount As Integer
                Dim buffer() As Byte = New Byte((bufferSize) - 1) {}
                readCount = ftpStream.Read(buffer, 0, bufferSize)

                While (readCount > 0)
                    outputStream.Write(buffer, 0, readCount)
                    readCount = ftpStream.Read(buffer, 0, bufferSize)

                End While
                ftpStream.Close()
                outputStream.Close()
                ftpresponse.Close()

                Dim responseFile As String = Server.MapPath("../ScannedDoc") + ("\\" + fileName)
                Response.ContentType = "application/pdf"
                Response.AppendHeader("Content-Disposition", ("attachment; filename=" + fileName))
                Response.TransmitFile(responseFile)
                Response.Flush()

                File.Delete(responseFile)

Thanks in advance
Sri.

Upvotes: 0

Views: 500

Answers (1)

Alexander
Alexander

Reputation: 2477

That behavior is by design. Point the download buttons to a separate page or handler which transmits the file so you don't have to leave the current page/request.

Upvotes: 2

Related Questions