2016-09-15 5 views
1

SQL Serverテーブルにvarbinary(max)として格納されているリポジトリからファイルをダウンロードできるようにするために、.netのgridviewを使用しようとしています。アップロードプロセスは問題ありませんでした。問題は検索にあります。私はファイルのダウンロードを許可するためにgridviewの各行にリンクボタンを使用します。 「ダウンロード」リンクボタンをクリックすると、セーブ/オープンボックス(IE)が表示されます。ただし、保存するファイルの名前は.aspxページの名前であり、テーブルに格納されているファイル名ではありません。ここで不適切なファイル名でasp.net(c#)のSQL ServerのBLOBを保存する

は、LinkBut​​tonコントロールのクリックイベントのコードです:

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

あなたは "disposition"(iを忘れています)というスペルを間違えました。それは次のようになります:

Response.AppendHeader("Content-Disposition", ("attachment; filename=" + strFilename)); 
関連する問題