程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c# 4 0-關於在gridview中實現下載功能的一點問題

c# 4 0-關於在gridview中實現下載功能的一點問題

編輯:編程綜合問答
關於在gridview中實現下載功能的一點問題

我用的而是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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved