4

When i am displaying image from folder using Handler.ashx ans then try to save the image by right clicking it it keeps giving me the option of "Save as type" asp.net generic handler and the handler name 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 behind.

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

Fhd.ashraf
  • 537
  • 7
  • 23

3 Answers3

5

You should set the ContentType and Content-Disposition HTTP headers before sending your data:

context.Response.ContentType = "image/png";
context.Response.Headers["Content-Disposition"] = "attachment; filename=yourfilename.png";
driis
  • 161,458
  • 45
  • 265
  • 341
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);

A good night sleep and your help did the trick i guess :) Thanks guys !

Fhd.ashraf
  • 537
  • 7
  • 23
0

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

Here is the code (in vb.net, sorry) that we use (I just verified that this does result in the correct file name for the user when requested from an 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
{
}
competent_tech
  • 44,465
  • 11
  • 90
  • 113