2011-11-11 8 views
4

Handler.ashxを使用してフォルダから画像を表示しているときに右クリックして画像を保存しようとすると、「名前を付けて保存」asp.netジェネリックハンドラとファイル名としてハンドラ名のオプションが与えられます。上記の。一般的なhandler.ashxにプロンプ​​ト "image name"として保存しますか?

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; 

です。私は画像の名前で保存する必要が

はhandler.ashxとしてではないANS

答えて

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をとContent-処分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

をユーザーがファイルを保存するには、 "application/octet-stream"のContentTypeを送信する必要があります。

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#:ここ

は、我々が使用するコード(vb.netで、申し訳ありません)(私はASHXから要求されたとき、これはユーザーのための正しいファイル名になるんていることが確認さ)であります翻訳:

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 
{ 
} 
関連する問題