Добавьте страницу универсального обработчика (.ashx) на свой веб-сайт. В приведенном ниже тексте кода Ashx показано, как прочитать произвольный поток (в данном случае файл PNG с диска) и записать его в ответ:
using System;
using System.Web;
using System.IO;
namespace ASHXTest
{
public class GetLetter : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// Get letter parameter from query string.
string fileName = context.Request.MapPath(string.Format("{0}.png",
context.Request.QueryString["letter"]));
// Load file from disk/database/ether.
FileStream stream = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
// Write response headers and content.
context.Response.ContentType = "image/png";
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
При желании вы также можете установить заголовок Content-Disposition
, как показано в ответе Хайнци:
context.Response.AddHeader("Content-Disposition",
"attachment;filename=\"letter.png\"");