62,046
社区成员
发帖
与我相关
我的任务
分享
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>");
}
}