当我使用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;
这是背后的代码。
我需要使用Image name ans而不是handler.ashx
来保存它答案 0 :(得分:5)
您应该在发送数据之前设置ContentType和Content-Disposition HTTP标头:
context.Response.ContentType = "image/png";
context.Response.Headers["Content-Disposition"] = "attachment; filename=yourfilename.png";
答案 1 :(得分: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);
晚安睡觉,你的帮助就是我猜的诀窍:)谢谢你们!
答案 2 :(得分:0)
当您希望用户保存文件时,您需要发送ContentType为“application / octet-stream”。
以下是我们使用的代码(在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
{
}