ASHX图像下载点击

atr*_*joe 1 c# asp.net ashx

我正在使用ashx来提供来自数据库的图像,无论如何都要让用户点击允许他们在计算机上下载文件的链接.(IE显示保存对话框)就像下载文件一样.这可能吗?

Kel*_*sey 6

如果您希望它提示保存,请确保在创建响应时添加以下行:

context.Response.AppendHeader("Content-Disposition",
    "attachment;filename=" + filename);
Run Code Online (Sandbox Code Playgroud)

这将使浏览器将其视为附件并使用保存对话框进行提示.

编辑:根据您的评论,确保您正确构建您的回复:

// set attachment header like above
// then you need to get your file in byte[] form
byte[] dataYouWantToServeUp = GetData();
// you can set content type as well
yourHttpContext.Response.ContentType = "image/jpg";
// serve up the response
yourHttpContext.Response.BinaryWrite(dataYouWantToServeUp);
Run Code Online (Sandbox Code Playgroud)