Save as a hint "image name" in general handler.ashx?

When I display an image from a folder using Handler.ashx ans, then try saving the image by right-clicking it, it continues to give me the option “Save as type” of asp.net generic handler and the name of the handler as the file 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); } } 

above is the handler and

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

this is the code.

I need to save it with ans image name not as handler.ashx

+4
source share
3 answers
  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); 

Good night sleep and your help did the trick I think :) Thanks guys!

+1
source

Before sending data, you must configure the HTTP headers ContentType and Content-Disposition:

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

If you want the user to save the file, you need to send ContentType "application / octet-stream".

Here is the code (in vb.net, sorry) that we are using (I just confirmed that this leads to the correct file name for the user when prompted from 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 # translation:

 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 { } 
0
source

Source: https://habr.com/ru/post/1380828/


All Articles