2014-01-20 33 views
0

我無法下載圖像上的按鈕點擊..我已經放置在網格按鈕下載各自的圖像..下面是我的代碼。圖像不下載按鈕點擊,即放置在網格內

protected void DownloadFile(object sender, EventArgs e) 
{ 
    int id = int.Parse((sender as LinkButton).CommandArgument); 
    byte[] bytes; 
    string fileName, contentType; 
    string constr = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(constr)) 
    { 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.CommandText = "select FileName,Extentions, FileContent from Documents where ID=" + id; 
      cmd.Parameters.AddWithValue("@Id", id); 
      cmd.Connection = con; 
      con.Open(); 
      using (SqlDataReader sdr = cmd.ExecuteReader()) 
      { 
       sdr.Read(); 
       bytes = (byte[])sdr["FileContent"]; 
       contentType = sdr["Extentions"].ToString(); 
       fileName = sdr["FileName"].ToString(); 
      } 
      con.Close(); 
     } 
    } 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.Charset = ""; 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.ContentType = contentType; 
    Response.AddHeader("Content-type", contentType); 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); 
    Response.BinaryWrite(bytes); 
    Response.Flush(); 
    Response.End(); 
} 
+0

請求/響應在Fiddler中看起來如何?它怎麼不起作用?我們需要更多的診斷信息。 – Amy

+0

代碼運行成功,從頭到尾..它沒有提供任何錯誤。但圖像不會下載。 – user3186679

回答

1

更改CommandText這樣:

cmd.CommandText = "select FileName,Extentions, FileContent from Documents where [email protected]"; 

與讀者閱讀到這一點:

while(sdr.Read()) 
{ 
       bytes = (byte[])sdr["FileContent"]; 
       contentType = sdr["Extentions"].ToString(); 
       fileName = sdr["FileName"].ToString(); 
} 

編輯:爲了測試,我用下面的代碼從獲取文件我的磁盤而不是數據庫:

protected void DownloadFile(object sender, EventArgs e) 
{ 
    int id = int.Parse((sender as LinkButton).CommandArgument); 
    byte[] bytes; 
    string fileName, contentType; 

    // Test code to download 123.sql file from C:\ 
    bytes = System.IO.File.ReadAllBytes("C:\\123.sql"); 
    contentType = ".sql"; 
    fileName = "123.sql"; 
    //string constr = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString; 
    //using (SqlConnection con = new SqlConnection(constr)) 
    //{ 
    // using (SqlCommand cmd = new SqlCommand()) 
    // { 
    //  cmd.CommandText = "select FileName,Extentions, FileContent from Documents where ID=" + id; 
    //  cmd.Parameters.AddWithValue("@Id", id); 
    //  cmd.Connection = con; 
    //  con.Open(); 
    //  using (SqlDataReader sdr = cmd.ExecuteReader()) 
    //  { 
    //   sdr.Read(); 
    //   bytes = (byte[])sdr["FileContent"]; 
    //   contentType = sdr["Extentions"].ToString(); 
    //   fileName = sdr["FileName"].ToString(); 
    //  } 
    //  con.Close(); 
    // } 
    //} 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.Charset = ""; 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.ContentType = contentType; 
    Response.AddHeader("Content-type", contentType); 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); 
    Response.BinaryWrite(bytes); 
    Response.Flush(); 
    Response.End(); 
} 

編輯2:Here是我用過的測試項目的鏈接。

+0

其不工作 – user3186679

+0

您必須調試。在'Response.BinaryWrite(bytes);'中放置一個斷點,並查看字節的值。如果它是空的(我懷疑是這樣),然後在'Response.BinaryWrite(bytes);'中設置一個斷點,獲取sql,用id值替換@Id並在sssms中運行查詢。 – afzalulh

+0

我已經檢查過它..它不爲空 – user3186679