首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请诸位大侠帮帮忙了,这样写为什么实现不了

2011-12-22 
请各位大侠帮帮忙了,这样写为什么实现不了?本人用fileupload控件做了一个上传的页面。运行成功后能将压缩文

请各位大侠帮帮忙了,这样写为什么实现不了?
本人用fileupload控件做了一个上传的页面。运行成功后能将压缩文件上传到项目根目录的upload文件夹中,同时将上传的文件名称,大小,类型等信息保存在一个为
t_upload的数据表中。
然后我又用GridView做了一个页面能显示上传的压缩文件的各项信息,并在GridView中添加了一个ButtonField列作为下载按钮。
我在GridView中增加了一个GridView1_RowCommand的事件
下面是我在该事件中的代码:

                string   sqlconn   =   "Data   Source=890A81AF51784ED;Initial   Catalog=lele;Integrated   Security=True ";
                SqlConnection   dconn   =   new   SqlConnection(sqlconn);
                SqlCommand   dcmd   =   new   SqlCommand();
                dcmd.Connection   =   dconn;
                dcmd.CommandText   =   "select   filename   from   t_upload ";
                dconn.Open();
                SqlDataReader   dr   =   dcmd.ExecuteReader();
                while   (dr.Read())
                {
                        ///get   the   file  
                        string   filepath   =   Server.MapPath( "~/upload/ "+dr[ "filename "].ToString   ()+ " ");
                        ///get   the   file   stream   to   get   the   file   length  
                        System.IO.FileStream   fs   =   new   System.IO.FileStream(filepath,   System.IO.FileMode.Open);
                        ///set   the   content   type  
                        Response.ContentType   =   "application/zip ";
                        ///set   Content-Disposition  
                        Response.AppendHeader( "Content-Disposition ",   "attachment;   filename= "+dr[ "filename "].ToString   ()+ " ");
                        ///get   the   file   size  
                        long   filesize   =   fs.Length;
                        fs.Close();
                        ///set   the   content   length   to   the   size   of   the   file  
                        ///this   will   chop   off   the   extra   junk   that   may   be   sent   by   the   ASP.NET   runtime   along   with   your   file  
                        Response.AddHeader( "Content-Length ",   filesize.ToString());


                        ///write   the   file   to   the   browser  
                        Response.WriteFile(filepath);
                        ///flush   it  
                        Response.Flush();
                }
                dr.Close();
                dconn.Close();

为什么点击下载按钮的时候,他只下载test.zip。
而没有从数据库中获得filepath、ContentType、ContentLength等信息
实现不同压缩包的下载。
请问我这样写有什么不对吗?



[解决办法]
<asp:GridView ID= "GridView1 " runat= "server " AutoGenerateColumns= "False " DataKeyNames= "id "
OnRowUpdating= "GridView1_RowUpdating ">
<Columns>
<asp:ButtonField CommandName= "update " Text= "下载 " />
</Columns>
</asp:GridView>
我用的是一个按钮列,设置CommandName= "update ",然后在GridView1的GridView1_RowUpdating
事件里写代码,同时设置DataKeyNames= "id "用来确定不同的文件,如果你的表里没id字段你也可以用DataKeyNames= "filename ",但后面的sql语句中的where条件就要改下

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = (int)GridView1.DataKeys[e.RowIndex].Value;//设置一个DataKeys
string sqlconn = "Data Source=890A81AF51784ED;Initial Catalog=lele;Integrated Security=True ";
SqlConnection dconn = new SqlConnection(sqlconn);
SqlCommand dcmd = new SqlCommand();
dcmd.Connection = dconn;
dcmd.CommandText = "select * from t_upload where id= " + id + " ";//前面的id是自动增加的字段,表里应该有吧
dconn.Open();
SqlDataReader dr = dcmd.ExecuteReader();
if (dr.Read())
{
///get the file
string filepath = Server.MapPath( "~/upload/ " + dr[ "filename "].ToString() + " ");
///get the file stream to get the file length
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.Open);
///set the content type
Response.ContentType = "application/zip ";
///set Content-Disposition
Response.AppendHeader( "Content-Disposition ", "attachment; filename= " + dr[ "filename "].ToString() + " ");
///get the file size
fs.Close();
///set the content length to the size of the file
///this will chop off the extra junk that may be sent by the ASP.NET runtime along with your file
Response.AddHeader( "Content-Length ", dr[ "filesize "].ToString());
///write the file to the browser
Response.WriteFile(filepath);
///flush it
Response.Flush();
}
dr.Close();
dconn.Close();
}

你试试行不行吧

热点排行