棘手问题,PDF输出,客户端DownLoad,当按下DownLoad提示窗口的“打开”Button后,DownLoad窗口会再次打开。(也就是说按“打开”的时候,DownLoad窗口会出现两次)如何解决,请大师指教!
以下是两段代码:
BtnOutput_Click 是输出Button的Click事件
Download 是PDF输出的函数
(注:Download函数里“p_Page.Response.End()”代码一直出错)
Private Sub BtnOutput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOutput.Click
        Dim dstValue As DataSet
        Dim dbTrnHakkousu As DB_TRN_Hakkou_su
        Dim srvPrintLog As SrvPrintFormLog
        Dim pdfCreate As PrintForm.PDFSchoolPublishList
        Dim objDownLoad As FileHandler
 
        Try
            dbTrnHakkousu = New DB_TRN_Hakkou_su
            dstValue = dbTrnHakkousu.FillDataToPDF()
            If dstValue.Tables(0).Rows.Count > 0 Then
                pdfCreate = New PrintForm.PDFSchoolPublishList(MyBase.FilePreservePath)
                pdfCreate.ExportFile(dstValue, Nothing)
                objDownLoad = New FileHandler
                objDownLoad.Download(Me, pdfCreate.FilePath)
            Else
                '
            End If
        Catch ex As Exception
            Me.ToMessageDialog(ex)
        Finally
            pdfCreate = Nothing
            srvPrintLog = Nothing
        End Try
    End Sub
Public Sub Download(ByVal p_Page As Page, ByVal p_FilePath As String, Optional ByVal p_Del As Boolean = True)
  Dim MyFileStream As New IO.FileStream(p_FilePath, IO.FileMode.Open, IO.FileAccess.Read)
  Dim Buffer(CInt(MyFileStream.Length)) As Byte
 
        Try
            MyFileStream.Read(Buffer, 0, CInt(MyFileStream.Length))
            MyFileStream.Close()
            MyFileStream = Nothing
 
            p_Page.Response.Clear()
            p_Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("Shift_JIS")
            Select Case Right(p_FilePath.Trim, 3).ToUpper
                Case "PDF"
                    p_Page.Response.ContentType = "application/pdf"
                Case "XLS"
                    p_Page.Response.ContentType = "application/x-msexcel"
                Case "TXT", "CSV"
                    p_Page.Response.ContentType = "text/plain"
                Case Else
                    p_Page.Response.ContentType = "application/octet-stream"
            End Select
            Dim str() As String = p_FilePath.Split("\"c)
            p_Page.Response.AddHeader("content-disposition", "attachment;filename =" & str(str.Length - 1))
            p_Page.Response.BinaryWrite(Buffer)
            Buffer = Nothing
            If p_Del Then IO.File.Delete(p_FilePath)
 
            p_Page.Response.End()
        Catch ex As Threading.ThreadAbortException
        Catch ex As Exception
            p_Page.Response.ClearContent()
            p_Page.Response.ClearHeaders()
            Throw ex
        End Try
 End Sub

解决方案 »

  1.   

    下载的文件是PDF格式时候是会出现你说的情况,我下载.xls格式的文件直接打开就没有问题,关注中!
      

  2.   

    string path = Server.MapPath(this.xlfile.Text+".xls"); System.IO.FileInfo file = new System.IO.FileInfo(path);
    Response.Clear();
    Response.Charset="GB2312";
    Response.ContentEncoding=System.Text.Encoding.UTF8;
    // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
    // 添加头信息,指定文件大小,让浏览器能够显示下载进度
    Response.AddHeader("Content-Length", file.Length.ToString());

    // 指定返回的是一个不能被客户端读取的流,必须被下载
    Response.ContentType = "application/ms-excel";

    // 把文件流发送到客户端
    Response.WriteFile(file.FullName);
    // 停止页面的执行

    Response.End();
      

  3.   

    也就是说按“打开”的时候,DownLoad窗口会出现两次 然后能下载吗
      

  4.   

    也就是说按“打开”的时候,DownLoad窗口会出现两次 然后能下载吗
     goody9807() ( ) :
    是的,其他一切正常,就是按“打开”的时候,DownLoad窗口会出现两次
      

  5.   

    问题解决:
    <form id="Form1" method="post" runat="server">
    改为
    <form id="Form1" method="get" runat="server">
    即可但想知道post和get的区别,默认下一般用post,用了get会有什么影响吗?
    测试程序如下:Dim strFilePath As String        strFilePath = "D:\TempFiles\111.pdf"        Dim MyFileStream As New IO.FileStream(strFilePath, IO.FileMode.Open, IO.FileAccess.Read)
            Dim Buffer(CInt(MyFileStream.Length)) As Byte        Try
                MyFileStream.Read(Buffer, 0, CInt(MyFileStream.Length))
                MyFileStream.Close()
                MyFileStream = Nothing            Response.Clear()
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("Shift_JIS")
                Select Case Right(strFilePath.Trim, 3).ToUpper
                    Case "PDF"
                        Response.ContentType = "application/pdf"
                    Case "XLS"
                        Response.ContentType = "application/x-msexcel"
                    Case "TXT", "CSV"
                        Response.ContentType = "text/plain"
                    Case Else
                        Response.ContentType = "application/octet-stream"
                End Select
                Dim str() As String = strFilePath.Split("\"c)
                Response.AddHeader("content-disposition", "inline;attachment;filename =AAA.pdf") '& str(str.Length - 1))
                Response.BinaryWrite(Buffer)
                Buffer = Nothing            Response.Flush()
                Response.End()
            Catch ex As Threading.ThreadAbortException
            Catch ex As Exception
            End Try