现在我实现了下载功能 可以用电脑自带的下载提示框下载;但是只能下载一次就结束foreach循环了,求高手指教! protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            string id = GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text.ToString();
            
            //遍历文件夹里面的所有文件
            string xlspath = string.Empty;
            string packagePath1 = Server.MapPath("../file/" + GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Text.ToString());
            string[] packageName = Directory.GetDirectories(packagePath1);
            foreach (string s in packageName)
            {
                //string extention = s.Substring(s.LastIndexOf('.') + 1);
                DataSet ds = DAL.SubjectDAL.getSubjectAllName();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string spath = s.Substring(s.LastIndexOf("\\") + 1);
                    if (spath == ds.Tables[0].Rows[i][0].ToString())
                    {
                        xlspath = spath;
                        string[] packagexlsName = Directory.GetFiles(s);
                        foreach (string s1 in packagexlsName)
                        {
                            string extention2 = s1.Substring(s1.LastIndexOf('.') + 1);
                            if (extention2 == "xls" || extention2 == "xml")
                            {
                                xlspath = s1;
                                if (e.CommandName == "download")//下载按钮绑定的事件
                                {
                                    try
                                    {
                                        if (xlspath == "" || xlspath == null)
                                        {
                                            MessageBox.Show(this.Page, "考卷信息不存在!");
                                            return;
                                        }
                                        else
                                        {
                                            //string download = Server.MapPath(xlspath);
                                            FileInfo info = new FileInfo(xlspath);
                                            if (info.Exists)
                                            {
                                                Response.Clear();
                                                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(info.Name));
                                                Response.AddHeader("Content-Length", info.Length.ToString());
                                                Response.ContentType = "application/octet-stream";
                                                //Response.Filter.Close();
                                                Response.WriteFile(info.FullName);
                                                //Response.End();
                                            }
                                            else
                                            {
                                                MessageBox.Show(this.Page, "考卷信息不存在!");                                            }
                                        }                                    }
                                    catch
                                    {
                                        MessageBox.Show(this.Page, "试卷信息丢失,联系管理员!");                                    }
                                }
                            }                        }
                    }
                }
            }
        }
        catch
        {
            MessageBox.Show(this.Page, "目标路径不存在");
        }
    }
/// <summary>
/// 弹出消息框
/// </summary>public class MessageBox
{
public MessageBox()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
    public static void Show(System.Web.UI.Page page,string strtext)
    {
        page.ClientScript.RegisterStartupScript(page.GetType(), "", "<script language=javascript>alert('" + strtext + "')</script>");
    }
}

解决方案 »

  1.   

    因为web服务器只能发送一次响应,因为http协议通讯都是无连接的,不可能阻塞线程
      

  2.   

    我想到一个思路,把文件列表发送到客户端,在客户端用js脚本循环遍历,然后提交到服务端,服务端根据js脚本指定的文件发送文件,这样,服务端还是只发送一个文件,具体的没试过,LZ自己尝试一下吧
      

  3.   

    http://www.cnblogs.com/baishahe/archive/2008/02/25/1080930.html具体没有弄过,你可以尝试把要下载的文件先进行压缩成一个文件(流),然后输出到客户端。
    可能涉及到的有:System.IO.Compression 命名空间  类:System.IO.Compression 命名空间包含提供基本的流压缩和解压缩服务的类。 
     类: DeflateStream         
     类:GZipStream             
     枚举:CompressionMode 具体
      

  4.   


    可以直接输出字节数组的,不一定要是媒体介质上的上的东西才能输出。
    this.Response.OutputStream.Write