我的程序中有一个生成Excel文件的功能, 我想做成文件生成后自动弹出下载对话框供用户下载文件,
如何处理这个自动弹出下载对话框呢 ?

解决方案 »

  1.   

    可以将生成的文件作为Stream写入Response.OutputStream,但是记住首先设置请求文件的类型为excel文件
      

  2.   

    string filepath = //存放文件路径;
    string filename = //生成Excel文件名;
    string path = filepath + filename;
    Response.Clear();
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
    Response.WriteFile(path);
    Response.End();
    放在你的生成代码后面,应该可以了
      

  3.   

    If System.IO.File.Exists(fileFullName) Then
     Try
                Dim myFile As New FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                Dim br As New BinaryReader(myFile)
                Try
                    _Response.AddHeader("Accept-Ranges", "bytes")
                    _Response.Buffer = True
                    Dim fileLength As Long = myFile.Length
                    Dim startBytes As Long = 0                Dim pack As Integer = 1024000 '1000K bytes
                    Dim sleep As Integer = CInt(Math.Floor((1000 * pack / _speed))) + 1
                    If Not (_Request.Headers("Range") Is Nothing) Then
                        _Response.StatusCode = 206
                        Dim range As String() = _Request.Headers("Range").Split(New Char() {"="c, "-"c})
                        startBytes = Convert.ToInt64(range(1))
                    End If
                    _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString())
                    If startBytes <> 0 Then
                        _Response.AddHeader("Content-Range", String.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength))
                    End If
                    _Response.AddHeader("Connection", "Keep-Alive")
                    _Response.ContentType = "application/octet-stream"
                    _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8))                br.BaseStream.Seek(startBytes, SeekOrigin.Begin)
                    Dim maxCount As Integer = CInt(Math.Floor(((fileLength - startBytes) / pack))) + 1                Dim i As Integer
                    For i = 0 To maxCount - 1
                        If _Response.IsClientConnected Then
                            _Response.BinaryWrite(br.ReadBytes(pack))
                        Else
                            i = maxCount
                        End If
                    Next i
                Catch ex As Exception
                    Throw New Exception(ex.ToString())
                Finally
                    br.Close()
                    myFile.Close()
                End Try
            Catch ex As Exception
                Throw New Exception(ex.ToString())
            End Try
            Else 
                RegisterStartupScript("filenotexist", "<script language='JavaScript'>alert('文件不存在!);</script>")
                Exit Sub
            End If