我用的而是mysql, 插入的是個button,在你的代碼基礎上,我做了點修改,但還是出錯,我點了下載之後,沒反應,也沒說哪裡有錯,看不出來啊
protected void Button1_Click(object sender, EventArgs e)
{
//獲取imgbtnDelete的Button對象
Button imgbtn = (Button)sender;
//引用imgbtnDelete控件的父控件上一級控件
GridViewRow gvr = (GridViewRow)imgbtn.Parent.Parent;
//獲取文件真實姓名
string sqlStr = "select FileName from downloadlist where FileID='" + GridView1.DataKeys[gvr.RowIndex+1].Value.ToString() + "'";
//打開數據庫
conn = new MySqlConnection(strSQL);
conn.Open();
MySqlDataAdapter dapt = new MySqlDataAdapter(sqlStr, conn);
DataSet ds = new DataSet();
dapt.Fill(ds, "downloadlist");
//獲取文件路徑
string strFilePath = Server.MapPath("DownLoad/" + ds.Tables["downloadlist"].Rows[0]["FileName"].ToString());
ds.Dispose();
conn.Close();
////下載指定的文件
if (File.Exists(strFilePath))
{
FileInfo fileInfo = new FileInfo(strFilePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + ds.Tables["downloadlist"].Rows[0]["FileName"].ToString());
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}
我是把下載文件都放在download這個文件夾中,每個下載文件的信息,我放在downloadlist這個表中,FileID是表的主鍵,我是想通過點擊哪一行的按鈕,然後這行的行號和表中的FileID有一個關聯,然後可以得到該行的文件名,然後寫到路徑裡面去,實現下載。我不知道路徑是不是有問題
![
![
http://bbs.csdn.net/topics/300190688