62,074
社区成员
发帖
与我相关
我的任务
分享
protected void Btn_Download_Click(object sender, EventArgs e)
{
int j = 0;
for (int i = 0; i < MyGrid.Rows.Count; i++)
{
GridViewRow row = MyGrid.Rows[i];
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)row.Cells[0].FindControl("cb_Choose");
if (cb.Checked)
{
j++;
//文件下载路径
string strPath = row.Cells[3].Text.ToString();
//下载文件
DownLoadFile(strPath);
}
}
}
protected void Btn_Download_Click(object sender, EventArgs e)
{
int j = 0;
for (int i = 0; i < MyGrid.Rows.Count; i++)
{
GridViewRow row = MyGrid.Rows[i];
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)row.Cells[0].FindControl("cb_Choose");
if (cb.Checked)
{
j++;
//文件下载路径
string strPath = row.Cells[3].Text.ToString();
//文件名
string strFileName = row.Cell[2].Text.ToString();
//下载文件
DownLoadFile(strPath, strFileName);
}
}
}
/// <summary>
/// 文件下载方法
/// </summary>
/// <param name="strPath">文件名</param>
private void DownLoadFile(string strPath, string strFileName)
{
if (!File.Exists(strPath))
{
Response.Write("<script>alert('服务器上已经没有此文件!');</script>");
return;
}
try
{
string filePath = strPath;
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, 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(strFileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
catch
{
Response.Write("<script>alert('查无此资料或已被删除');</script>");
return;
}
}
我即使全部选中, 也只是下载一个文件。调试发现, Response.End();运行完后就跳转到catch里去了
请教,是不是只能下载一个文件??
如果这样的话,我用radiobutton就行了,都不用checkbox了
<asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="False"
Width="700px"
border="0" cellpadding="0" cellspacing="1" class="tableborder" >
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="cb_Choose" runat="server" Width="40px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Doc_Name" HeaderText="文档名称">
<ItemStyle HorizontalAlign="Left" Width="300px" />
</asp:BoundField>
<asp:BoundField DataField="Doc_Type_Name" HeaderText="文档类型">
<ItemStyle HorizontalAlign="Center" Width="80px" />
</asp:BoundField>
<asp:BoundField DataField="SavePath" HeaderText="保存路径">
<ItemStyle HorizontalAlign="Left" Width="300px" />
</asp:BoundField>
<asp:BoundField DataField="Upload_Operator" HeaderText="上传者">
<ItemStyle HorizontalAlign="Center" Width="60px" />
</asp:BoundField>
<asp:BoundField DataField="Upload_Time" HeaderText="上传时间">
<ItemStyle HorizontalAlign="Center" Width="150px" />
</asp:BoundField>
</Columns>
</asp:GridView>
这是前台gridview的代码