2011-04-06 91 views
1

當用戶單擊我的鏈接時,它會彈出另存爲對話框,但它想要將其另存爲除IE之外的其他瀏覽器中的ashx文件。該文件是從數據庫中提取的標準jpeg。什麼會導致這樣做?ASHX圖像下載保存爲ASHX

 string id = ctx.Request.QueryString["id"]; 

     SqlConnection con = new SqlConnection(); 
     SqlCommand cmd = new SqlCommand("EBig", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@EID", id); 

     byte[] pict = null; 
     ctx.Response.ContentType = "image/pjpeg"; 
     try 
     { 
      con.Open(); 
      pict = (byte[])cmd.ExecuteScalar(); 

      ctx.Response.Clear(); 

      ctx.Response.AppendHeader("content-disposition", "attachment;"); 
      ctx.Response.ContentType = "image/pjpeg"; 
      ctx.Response.BinaryWrite(pict); 
      ctx.Response.Flush(); 
      ctx.Response.End(); 
     } 
     catch 
     { 

     } 
     finally 
     { 
      con.Close(); 
     } 

回答

3

Hanselman has a post對此。它涉及在您的Content-Disposition響應標頭上設置名爲filename的附加屬性。

的相關RFC is here,相關部爲2.3:

發送者可能希望提供一個文件名中使用如果實體是 分離並存儲在單獨的文件中。如果接收MUA將 實體寫入文件,則在可能的情況下,建議的文件名應當用作實際文件名的 基礎。

0

您需要添加的文件名:

context.Response.AppendHeader("Content-Disposition", 
    string.Format("attachment;filename={0}", yourFileName));