C#中用gridview实现下载功能,会的来 只要能解决,加分不成问题 50

前台代码:<asp:GridViewID="GridView1"runat="server"AllowPaging="True"AllowSorting="True"Au... 前台代码:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Width ="100%" Height ="100%"
DataKeyNames="id" DataMember="DefaultView"
onrowcommand="GridView1_RowCommand1">
<Columns>
<asp:BoundField DataField="id" HeaderText="序号" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="Name" HeaderText="文件名" SortExpression="Name" />
<asp:BoundField DataField="Typ" HeaderText="语言类型" SortExpression="Type2" />
<asp:BoundField DataField="Instr" HeaderText="文件简介" SortExpression="Instr" />
<asp:BoundField DataField="Time" HeaderText="上传时间" SortExpression="Time" />
<asp:ButtonField CommandName="Down" Text="下载" SortExpression="id"
ValidationGroup="id" />
</Columns>
<SelectedRowStyle ForeColor="#FF9999" > </asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [id], [Name], [Typ], [Instr], [Time] FROM [Play]">
</asp:SqlDataSource>
后台代码:
int index = Convert.ToInt32(e.CommandArgument);
SqlConnection conn = new SqlConnection("Data Source=localhost;Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Best.mdf;Integrated Security=True;User Instance=True");
string sql = "select Name from [Play] where id=@index";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@index", index.ToString());
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
string fileName = Convert.ToString(this.GridView1.Rows[0]);
string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);//得到文件的后缀名
string filePath = Server.MapPath("..\\Play\\Upload\\" + fileName + fileType); long fileSize = fileStream.Length + 100000;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=..\\Play\\Upload\"" + UTF_FileName(fileName) + fileType + "\";");
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fileStream.Close();
HttpContext.Current.Response.End();
这个问题积累好久,希望有热心的各位帮我搞定。问题就在于当点击下载时会出现另存为对话框但是不能正确读取文件名和类型,帮帮忙啊,由于代码比较多,提交时忘了选择分数,起步价50,封顶200,愿意帮忙的就帮吧
展开
 我来答
百度网友16f1cd8
2011-03-22
知道答主
回答量:28
采纳率:0%
帮助的人:11.2万
展开全部
//下载文件时候调用方法
public static void FileDownload(string FileName)
{
String FullFileName = System.Web.HttpContext.Current.Server.MapPath(@"../test/" + FileName);
FileInfo DownloadFile = new FileInfo(FullFileName);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
//用一个复选框选择然后获取文件名字
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
string name = GridView1.Rows[i].Cells[2].Text.ToString();
//Response.Write(name);
FileDownload(name);
}

}

}
追问
不行啊,又出现新的错误:编译器错误消息: CS0115: “ASP.play_download_aspx.GetTypeHashCode()”: 没有找到适合的方法来重写

行 1446: [System.Diagnostics.DebuggerNonUserCodeAttribute()]
行 1447: public override int GetTypeHashCode() {
行 1448: return -1536287560;
行 1449: }
这又是什么错误?帮忙解决啊,分不是问题呀
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2011-03-23
展开全部
可以不用下载功能啊
呵呵
开玩笑
祝你早日弄明白
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式