在通用处理程序.ashx中,保存为提示“图像名称”?

4

当我使用Handler.ashx从文件夹中显示图像,然后尝试通过右键单击它保存图像时,它会一直给我提供“另存为类型”选项的ASP.NET通用处理程序和处理程序名称作为文件名。

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处理程序。

3个回答

5

在发送数据之前,您应该设置ContentType和Content-Disposition HTTP头:

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

我已经尝试了所有的方法,仍然得到相同的错误!已经浪费了近两个小时! - Fhd.ashraf
@Fhd,但我在您发布的成本中没有看到这一点。 - Joel Coehoorn
1
文件名应该放在引号中:filename=\"yourfilename.png\",否则Firefox可能会出现问题。至于使右键单击=>“另存为”起作用-没有机会这样做。 - Michael Stum

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);

晚上好的睡眠和你们的帮助解决了问题,谢谢大家!


0

当您希望用户保存文件时,需要发送“application/octet-stream”的ContentType。

以下是我们使用的代码(使用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
{
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接