我有一组pdf文件存储在只能由应用程序访问的位置 - 所以这些文件不能直接通过http访问.
文件路径由数据库存储,当给予用户下载文件的选项时,正在执行以下代码:
Response.ContentType = "Application/pdf"
Response.AppendHeader("Content-Disposition", "attachment; filename=<some file name>")
Response.TransmitFile(Server.MapPath("<the file path>"))
Response.End()
Run Code Online (Sandbox Code Playgroud)
如果我处理单个文件,一切正常
问题:
我有一个存储过程,返回用户可供下载的所有文件的列表,它可以返回任意数量的文件,范围从1到20.
我想创建一个页面,列出所有这些文件以及每个文件旁边的下载选项.
就像是
File name 1, some description 1, download (button, link?)
File name 2, some description 2, download (button, link?)
Run Code Online (Sandbox Code Playgroud)
它不能只是一个url链接,因为正如我所提到的,文件不能直接访问,并且每次下载都需要执行一些代码
使用.net2.0实现这一目标的优雅解决方案是什么?
使用常规ASP.NET(2.0),您可能希望创建一个"处理程序".最简单的选择是添加"通用处理程序"(ashx)模板 - 然后在页面上放置常规链接(例如)"download.ashx?id = 2".
在处理程序内部,解析查询字符串,并将文件流式传输给用户:
public void ProcessRequest(HttpContext context)
{
// set headers...
string filePath = FindPath(context.Request.QueryString("id"));
context.Response.WriteFile(filePath);
}
Run Code Online (Sandbox Code Playgroud)
其中FindPath从查询字符串链接解析物理文件.
如果这是ASP.NET MVC,您可以使用File(path)action-result.
| 归档时间: |
|
| 查看次数: |
1965 次 |
| 最近记录: |