给你一段
if (e.CommandName == "Selectupdown")
{
string sqlupdown = "select * from contractside where id="+ int.Parse(e.Item.Cells[0].Text.Trim()) +"";
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["sqlconnstring"]);
conn.Open();
SqlCommand cmd = new SqlCommand(sqlupdown,conn);
SqlDataReader drupdow = cmd.ExecuteReader(); 
if (drupdow.Read())
{
Response.ContentType = "application/x-msdownload";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(drupdow["name"].ToString().Trim()) + ""+ drupdow["expandname"].ToString().Trim() +"");
Response.BinaryWrite((byte[])drupdow["side"]);
Response.Flush();
Response.End();
RegisterStartupScript("alert", "<script language='javascript'> alert('附件成功下载!')</script>");
}
drupdow.Close();
conn.Close();
}

解决方案 »

  1.   

    Dim oFile As FileInfo = New FileInfo(strFilePath)    
           With Response
                    .Clear()           
                    .ClearHeaders()
                    .Buffer = False
                    .AddHeader("Content-Disposition:", "attachment; filename=" & System.Web.HttpUtility.UrlEncode(oFile.FullName))
                    .AddHeader("Content-Length", oFile.Length.ToString())
                    .Charset = "GB2312"
                    .ContentType = "APPLICATION/vnd.ms-excel"
                    .ContentType = "application/octet-stream"
                    .WriteFile(oFile.FullName)
                    .Flush()
                    .End()
                   
                End With
      

  2.   

    比如我要输出一个*.doc文件:
    string strFile = "e:\\abc.doc";
    FileStream fs = new FileStream(strFile, FileMode.Open);
    byte[] bytes = new byte[(int)fs.Length];
    fs.Read(bytes, 0, bytes.Length);
    fs.Close();
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition",  "attachment;  filename="  +  HttpUtility.UrlEncode(File.Name)) ;
    Response.BinaryWrite(bytes);
    Response.End();
      

  3.   

    Dim oFile As FileInfo = New FileInfo(strFilePath)    
           With Response
                    .Clear()           
                    .ClearHeaders()
                    .Buffer = False
                    .AddHeader("Content-Disposition:", "attachment; filename=" & System.Web.HttpUtility.UrlEncode(oFile.FullName))
                    .AddHeader("Content-Length", oFile.Length.ToString())
                    .Charset = "GB2312"
                    .ContentType = "APPLICATION/vnd.ms-excel"
                    .WriteFile(oFile.FullName)
                    .Flush()
                    .End()
                   
                End With
    刚才发多了一段
      

  4.   

    文件下载:   
     
    To force the browser to display the Download dialog, you need to set the Response object's ContentType property to application/octet-stream and add an HTTP Request header called Content-Disposition with the following value: attachment; filename="filename" where filename is the default filename that will be displayed in the Download dialog at the browser. Therefore, to send a file to the browser, the first thing to do is to write the following code: 
    Response.ContentType = "application/octet-stream"
    Response.AddHeader("Content-Disposition", _
      "attachment; filename=""" & filename & """")I will give you a  example:
    <%@ Import Namespace="System.IO"%>
    <html>
    <head>
    <title>File Download</title>
    <script language="VB" runat="server">
    Sub Page_Load(sender As Object, e As EventArgs)
     
      Dim root As String = "C:\temp\"
     
      Dim files() As String = Directory.GetFiles(root)
      Dim sb As New StringBuilder(2048)
      Dim f As String
      For Each f In files
        Dim filename As String = Path.GetFileName(f)
        sb.Append("<br><a href=FileDownload.aspx?file=")
        sb.Append(root).Append(Server.UrlEncode(filename))
        sb.Append(">").Append(filename).Append("</a>")
      Next
      fileList.Text = sb.ToString()End Sub</script>
    </head>
    <body>
    <form runat="server">
    <asp:Label id="fileList" runat="server"/>
    </form>
    </body>
    </html>
      

  5.   

    最简单也是最常用的办法就是把这些文件压缩成rar就行了
      

  6.   

    可是我因为是在DataGrid的响应事件里面进行下载的,
    那我就要写成像 objDataGrid.Page.Response…… 这样了。这样的确可以实现下载功能,问题是这样访问Page会引起其他java script的问题,比如不允许访问window.event不知道有没有人遇到过这问题,是怎么解决的?
      

  7.   

    用javascript
    Public Sub OpenOrDownloadFile(ByVal FileName As String)
            Dim context As System.Web.HttpContext = System.Web.HttpContext.Current
    '必须用URL路径
           Dim Fname As String = "http://" & Request.UrlReferrer.Host + "/" + Request.Url.Segments(1) + "Exported/" + FileName 
            Dim sb As StringBuilder = New StringBuilder("<script language='javascript'>")
            sb.Append("window.open('" + Fname + "',target='_blank','height=" + Session("ClientScreenHeight") + ",width=" + Session("ClientScreenWidth") + ",left=0,top=0,status=yes,toolbar=yes,menubar=yes,location=no,resizable=yes,scrollbars=yes')")
            'sb.Append("{window.open('" + Fname + "',null,'height=" + Session("ClientScreenHeight") + ",width=" + Session("ClientScreenWidth") + ",left=0,top=0');}")
            sb.Append("</script>")
            context.Response.Write(sb.ToString())
        End Sub
      

  8.   

    要实现一个下载文件的程序.
    但通常提供文件链接的实现方法,在下载文件时,如图片文件,文本文件,HTML文件或Word文件等,浏览器会自动调用相关程序直接打开要下载的文件.
    我想做到下载任何文件时都能弹出下载对话框,不知道有什么办法可以实现?如果想下载的话点右键就可以了,
    象你说的那些文件图片文件,文本文件,HTML文件用户会不会一般想直接看看就可以了呢?
      

  9.   

    Response.Clear();
    Response.Buffer= true;
    Response.ContentType = "application/vnd.ms-word";
    Response.Charset = "";
    this.EnableViewState = false;
    Response.ContentEncoding =System.Text.Encoding.UTF8; System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); //this.ClearControls(dg);
    DataGrid1.RenderControl(oHtmlTextWriter);//dg是DataGrid Response.Write(oStringWriter.ToString()); Response.End();
      

  10.   

    单单是弹出下载对话框的功能实现了,但是因为我的是在响应DataGrid的ItemCommand事件里面实现的,就引起了其他java script的问题。不知道有没有人遇见过?http://expert.csdn.net/Expert/topic/2602/2602493.xml?temp=1.562136E-02
      

  11.   

    Response.AddHeader("Content-Disposition", _
      "attachment; filename=""" & filename & """")关键就是这句,attachment告诉游览器下载,不是打开