2016-09-15 29 views
1

我想在.net中使用gridview來允許用戶從存儲在sql server表中作爲varbinary(max)的存儲庫中下載文件。上傳過程沒有問題。問題在於檢索。我在gridview的每一行使用linkbutton來允許下載文件。當點擊「下載」鏈接按鈕時,我會看到一個保存/打開框(IE)。但是,要保存的文件的名稱是.aspx頁面的名稱,而不是表中存儲的文件名。從asp.net(c#)中的SQL Server中的Blob保存錯誤的文件名

下面是LinkBut​​ton的Click事件的代碼:我是新來存儲/從表中檢索斑點和我親近,只是存儲在文件系統中的文件

protected void lbtnDownload_Click(object sender, EventArgs e) 
    { 
     int gmsrId = int.Parse((sender as LinkButton).CommandArgument); 

     byte[] bytes; 
     string strFilename = String.Empty; 
     string strContentType = String.Empty; 

     string strConnString = dbUtilities.GetConnectionString(); 

     using (SqlConnection sqlCon = new SqlConnection(strConnString)) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       cmd.CommandType = CommandType.StoredProcedure; 
       cmd.CommandText = "SP_SelectDocument"; 
       cmd.Connection = sqlCon; 

       cmd.Parameters.AddWithValue("@intID", gmsrId); 
       sqlCon.Open(); 

       using (SqlDataReader sdrDocument = cmd.ExecuteReader()) 
       { 

        sdrDocument.Read(); 
        bytes = (byte[])sdrDocument["gmsr_Document"]; 
        strContentType = sdrDocument["gmsr_ContentType"].ToString(); 
        strFilename = sdrDocument["gmsr_Filename"].ToString(); 
        lblMessage.Text = "Filename = " + strFilename; 
        sqlCon.Close(); 
       } 

       Response.Clear(); 
       Response.Buffer = true; 
       Response.Charset = ""; 
       Response.Cache.SetCacheability(HttpCacheability.NoCache); 
       Response.ContentType = strContentType; 

       Response.AppendHeader("Content-Dispositon", ("attachment; filename=" + strFilename)); 
       Response.BinaryWrite(bytes); 
       Response.Flush(); 
       Response.End(); 


      } 
     } 
    } 

,但我不想給向上。我想我錯過了一些小東西,但找不到它。提前致謝。

+0

應該避免使用sp_前綴。它由MS保留,意味着系統過程,而不是存儲過程。 http://sqlperformance.com/2012/10/t-sql-queries/sp_prefix個人而言,我不喜歡所有的前綴,因爲除了不必要的擊鍵和查找列表中的對象的困難之外,它們什麼都不加。 –

回答

3

你拼錯了「性格」(忘了我)。它應該是:

Response.AppendHeader("Content-Disposition", ("attachment; filename=" + strFilename)); 
相關問題