我在 SQL Server 中存储了一些 MS Word 文档。当我想下载它们时,我首先将它们保存为服务器上的文件,然后用户可以下载它们,但我正在寻找一种不涉及将文件保存到服务器文件系统的方法......
问问题
1233 次
1 回答
2
您将需要一个实现 IHttpHandler 的自定义 HttpHandler。请参阅MSDN 中的IHttpHandler.ProcessRequest 方法。设置适当的 MIME 类型并将二进制数据从数据库重定向到 HttpContext 的 OutputStream。
名为Docs.ashx的自定义处理程序的示例(猜测,未测试):
using System.IO;
using System.Web;
public class Docs : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
using (var streamReader = new StreamReader("fileSourceForDemonstration.docx"))
{
streamReader.BaseStream.CopyTo(context.Response.OutputStream);
}
}
public bool IsReusable
{
get { return false; }
}
}
于 2011-10-11T12:25:27.097 回答