2011-11-11 29 views
4

當我使用Handler.ashx ans顯示文件夾中的圖像時,然後嘗試通過右鍵單擊保存圖像,它不斷給我選擇「另存爲類型」asp.net通用處理程序和處理程序名稱作爲文件名。 。上述另存爲通用handler.ashx中的提示「圖像名稱」?

Bitmap target = new Bitmap(width, height); 
    using (Graphics graphics = Graphics.FromImage(target)) { 
     graphics.CompositingQuality = CompositingQuality.HighSpeed; 
     graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     graphics.CompositingMode = CompositingMode.SourceCopy; 
     graphics.DrawImage(photo, 0, 0, width, height); 
     using (MemoryStream memoryStream = new MemoryStream()) { 
      target.Save(memoryStream, ImageFormat.Png); 
      OutputCacheResponse(context, File.GetLastWriteTime(photoPath)); 
      using (FileStream diskCacheStream = new FileStream(cachePath, FileMode.CreateNew)) 
      { 
       memoryStream.WriteTo(diskCacheStream); 
      } 
      memoryStream.WriteTo(context.Response.OutputStream); 
     } 
    } 

是處理程序和

ImageTiff.ImageUrl = "Handler.ashx?p=" + Parameter; 

這是後面的代碼。

我需要將其與圖片名稱保存ANS不如handler.ashx

回答

1
  context.Response.ContentType = "image/pjpeg"; 
      context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + photoName + "\""); 

      OutputCacheResponse(context, File.GetLastWriteTime(photoPath)); 

      context.Response.Flush(); 


      using (FileStream diskCacheStream = new FileStream(cachePath, FileMode.CreateNew)) 
      { 
       memoryStream.WriteTo(diskCacheStream); 
      } 
      memoryStream.WriteTo(context.Response.OutputStream); 

一個良好的睡眠和你的幫助確實起作用我想:)謝謝你們!

5

必須在發送數據之前設置的ContentType和內容處置HTTP標頭:

context.Response.ContentType = "image/png"; 
context.Response.Headers["Content-Disposition"] = "attachment; filename=yourfilename.png"; 
+0

我已經試過了所有的兄弟,但是我得到了同樣的錯誤!!浪費了將近2個小時! –

+0

@Fhd,但我沒有看到你發佈的任何地方 –

+1

文件名應該用引號:'filename = \「yourfilename.png \」',否則Firefox可能會導致問題。至於使Rightclick =>保存目標爲工作 - 沒有機會這樣做。 –

0

當你想用戶保存文件,您需要發送ContentType爲「application/octet-stream」。

下面是代碼(在vb.net,對不起),我們使用(我只是驗證,這樣做導致用戶正確的文件名從一個ASHX要求時):

With context 
     Try 
      ' Remove what other controls may have been put on the page 
      .ClearContent() 
      ' Clear any headers 
      .ClearHeaders() 
     Catch theException As System.Web.HttpException 
      ' Ignore this exception, which could occur if there were no HTTP headers in the response 
     End Try 

     .ContentType = "application/octet-stream" 
     .AddHeader("Content-Disposition", "attachment; filename=" & sFileNameForUser) 

     .TransmitFile(sFileName) 

     ' Ensure the file is properly flushed to the user 
     .Flush() 

     ' Ensure the response is closed 
     .Close() 

     Try 
      .End() 
     Catch 
     End Try 

    End With 

C#翻譯:

try 
{ 
     // Remove what other controls may have been put on the page 
    context.ClearContent(); 
     // Clear any headers 
    context.ClearHeaders(); 
} 
catch (System.Web.HttpException theException) 
{ 
     // Ignore this exception, which could occur if there were no HTTP headers in the response 
} 

context.ContentType = "application/octet-stream"; 
context.AddHeader("Content-Disposition", "attachment; filename=" + sFileNameForUser); 

context.TransmitFile(sFileName); 

    // Ensure the file is properly flushed to the user 
context.Flush(); 

    // Ensure the response is closed 
context.Close(); 

try 
{ 
    context.End(); 
} 
catch 
{ 
} 
相關問題