另存为提示“图像名称”在通用 handler.ashx 中?

发布于 2024-12-15 01:45:49 字数 1101 浏览 5 评论 0原文

当我使用 Handler.ashx ans 显示文件夹中的图像时,然后尝试通过右键单击图像来保存图像,它会不断向我提供“另存为类型”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 保存它

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开雨落又逢春i 2024-12-22 01:45:49

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

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

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";
满身野味 2024-12-22 01:45:49
          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);

睡个好觉,我猜你的帮助成功了:) 谢谢大家!

          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 !

煮茶煮酒煮时光 2024-12-22 01:45:49

当您希望用户保存文件时,您需要发送“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
{
}

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
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文