现在我实现了下载功能 可以用电脑自带的下载提示框下载;但是只能下载一次就结束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>");
}
}
{
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>");
}
}
可能涉及到的有:System.IO.Compression 命名空间 类:System.IO.Compression 命名空间包含提供基本的流压缩和解压缩服务的类。
类: DeflateStream
类:GZipStream
枚举:CompressionMode 具体
可以直接输出字节数组的,不一定要是媒体介质上的上的东西才能输出。
this.Response.OutputStream.Write