我正在一个需要上传/下载功能的网站上工作。上传工作正常,但是当我按下下载上传的文件时,几乎没有任何反应。
//Upload
protected void btnUpload_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
fileUpload1.SaveAs(Server.MapPath("Files/" + filename));
con.Open();
SqlCommand cmd = new SqlCommand("insert into FilesTable(FileName,FilePath) values(@Name,@Path)", con);
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@Path", "Files/" + filename);
cmd.ExecuteNonQuery();
con.Close();
BindGridviewData();
}
//Download
protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e)
{
SqlCommand com = new SqlCommand("select FileName,FilePath from FilesTable where Id=@Id", con);
com.Parameters.AddWithValue("Id", gvDetails.SelectedRow.Cells[1].Text);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = dr["type"].ToString();
Response.AddHeader("content-disposition", "attachment;filename=" + dr["Name"].ToString());
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite((byte[])dr["data"]);
Response.End();
}
}