2012-11-26 21 views
0

我有一個fileupload控件將圖像上傳到SQL Server 2008數據庫,我想用保存對話框彈出框下載它。下載文件的代碼執行得很好,但是當我點擊下載鏈接按鈕時沒有任何反應。該事件源於後面的代碼。我唯一的猜測是它是導致問題的又一次模式彈出。從ASP.NET C#頁面上的SQL Server下載圖像

<Modalpopup> 
</ModalPopup> 
<UpdatePanel> 
    --DetailsView 
    --------LinkButton(Download) 
    --DetailsView 
</UpdatePanel> 

是頁面的結構。

<!-- Details View here --> 
      <asp:DetailsView ID="dvReviewCases" runat="server" Height="50px" 
       AutoGenerateRows="False" CssClass="dvCSS" 
       OnDataBound="dvADReviewCases_DataBound" 
       onpageindexchanging="dvReviewCases_PageIndexChanging" onitemcommand="dvReviewCases_ItemCommand" 
       > 
       <Fields> 

        <asp:TemplateField HeaderStyle-CssClass="dvHeaderStyle" HeaderText="Evidence" ItemStyle-CssClass="dvValueField" > 
         <ItemTemplate> 
          <asp:LinkButton ID="btnDownload" runat="server" Text='<%#Eval("evidenceID") %>' CommandName="Download" > 
          </asp:LinkButton> 
         </ItemTemplate>  
        </asp:TemplateField> 


       </Fields> 
      </asp:DetailsView> 

代碼隱藏

// Download file from Evidence table 
    protected void dvReviewCases_ItemCommand(object sender, DetailsViewCommandEventArgs e) 
    { 
     if (e.CommandName == "Download") 
     { 
      String strCaseID = Session["caseID"].ToString(); 
      String strEvidenceID = Session["evidenceID"].ToString(); 

      //Search for evidence file 
      DbCommand cmd = GetData.CreateCommand("ADGetEvidence"); 
      GetData.AddParameter(cmd, strEvidenceID, "@evidenceID"); 
      GetData.AddParameter(cmd, strCaseID, "@caseID"); 
      GetData.GetResults(cmd); 

      // Check if datatable has row returned 
      if (GetData.getTable().Rows.Count > 0) 
      { 
       String fileName = "Evidence" + Session["caseID"].ToString(); 
       byte[] file = null; 
       String fileType = GetData.getTable().Rows[0][1].ToString(); 

       // Typecast resulting row to byte 
       file = ((byte[])GetData.getTable().Rows[0][0]); 
       Response.ContentType = fileType; 

       // Give user option to download file 
       Response.AddHeader("Content-Disposition","attachment;filename=" + fileName); 
       Response.BinaryWrite(file); 

       Response.End(); 

      } 

     } 
    } 
+0

我試着加入btnDownload作爲更新面板的觸發器,但它給了我一個錯誤,它無法找到該按鈕時,我跑了頁。 – ReiRei

回答

0

回傳從UpdatePanel內只能用整版迴應,作爲MSAjax魔術合併舊頁面(瀏覽器)和DIFF由回傳生成呈現新版本。

將LinkBut​​ton轉換爲具有所有必需參數的HyperLink並實現HttpHandler以提供二進制數據。

1

爲什麼不嘗試使用查詢字符串。這裏是我的代碼,在查詢字符串中使用,我從數據庫中獲取圖像,而不必在您的本地文件夾中創建任何想要的圖像的instatnce。跳這有助於。

<%@ WebHandler Language="C#" Class="DisplayImg" %> 

using System; 
using System.Web; 
using System.Configuration; 
using System.IO; 
using System.Data; 
using System.Data.SqlClient; 

public class DisplayImg : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     string theID; 
     if (context.Request.QueryString["id"] != null) 
      theID = context.Request.QueryString["id"].ToString(); 
     else 
      throw new ArgumentException("No parameter specified"); 

     context.Response.ContentType = "image/jpeg"; 
     Stream strm = DisplayImage(theID); 
     byte[] buffer = new byte[2048]; 
     int byteSeq = strm.Read(buffer, 0, 2048); 

     while (byteSeq > 0) 
     { 
      context.Response.OutputStream.Write(buffer, 0, byteSeq); 
      byteSeq = strm.Read(buffer, 0, 2048); 
     } 
    } 

    public Stream DisplayImage(string theID) 
    { 
     SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SERVER"].ConnectionString.ToString()); 
     string sql = "SELECT Server_image_icon FROM tbl_ServerMaster WHERE server_Code = @ID"; 
     SqlCommand cmd = new SqlCommand(sql, connection); 
     cmd.CommandType = CommandType.Text; 
     cmd.Parameters.AddWithValue("@ID", theID); 
     connection.Open(); 
     object theImg = cmd.ExecuteScalar(); 
     try 
     { 
      return new MemoryStream((byte[])theImg); 
     } 
     catch 
     { 
      return null; 
     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

只需添加一行在CS代碼

UploadImg.ImageUrl = "~/DisplayImg.ashx?id=" + code; 
+0

我能夠通過將LinkBut​​ton放入UpdatePanel中並將該按鈕註冊爲PostBack觸發器來修復我的代碼。但是,我現在的問題是,當我下載文件時,它只返回1字節的數據。 – ReiRei

相關問題