本人用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等信息
实现不同压缩包的下载。
请问我这样写有什么不对吗?
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等信息
实现不同压缩包的下载。
请问我这样写有什么不对吗?
原代码是这样的
protected void Page_Load(object sender, EventArgs e)
{
///get the file
string filepath = Server.MapPath("test.zip");
///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=test.zip");
///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();
}
我测试了
可以成功。
例如:
///get the file
string filepath = Server.MapPath("test.zip");
中路径MapPath可以从数据库中获得。
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();
}你试试行不行吧
可为什么有些包的filename是那中看不动的东东?
好象是繁体乱马
去天涯发个帖子感谢感谢!!我在Page_Load中写了显示整个数据表
为什么它就显示了一个列 “下载”,怎么显示t_upload表?