页面执行/生命周期在Response.TransmitFile之后停止

时间:2013-06-07 02:13:25

标签: c# asp.net .net vb.net

我有每行的gridview和下载按钮。 页面目标是列出与物理文件相关的所有记录,当点击下载按钮时,它应该下载并打开下载的文件。文件采用'pdf'格式。

页面已编码,一切正常。我想在下载文件时重新绑定gridview。但是在下载之后,页面的其余部分的执行或生命周期都没有完成。

似乎问题在于页面处理程序。我尝试通过互联网搜索,但遗憾的是没有找到解决方案。我请你们专家请帮我解决这个问题。 我的代码如下所示。

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)

提前致谢
斯里兰卡。

1 个答案:

答案 0 :(得分:2)

这种行为是设计的。将下载按钮指向发送文件的单独页面或处理程序,这样您就不必离开当前页面/请求。