我想在特定文件夹中显示一个文件列表作为超链接,当用户点击链接时,它会打开相应的文件以提示用户下载或查看,但是一直有错误说:< / p>
{"Value cannot be null.\r\nParameter name: filename"}
在context.Response.WriteFile(context.Request.QueryString["files"]);
行
我尝试了很多方法但无济于事。
ASHX文件:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var directory = new DirectoryInfo("C:\\temp\\Safety&Security");
var files = (from f in directory.GetFiles()
orderby f.LastWriteTime descending
select f).First();
//string[] files = Directory.GetFiles(@"C:\temp\Safety&Security");
//files = files.Substring(files.LastIndexOf(("\\")) + 1);
rpt.DataSource = files;
rpt.DataBind();
}
if (!Page.IsPostBack)
{
string[] files = Directory.GetFiles(@"C:\temp\marketing");
Repeater1.DataSource = files;
Repeater1.DataBind();
}
if (!Page.IsPostBack)
{
string[] files = Directory.GetFiles(@"C:\temp\IT");
Repeater2.DataSource = files;
Repeater2.DataBind();
}
}
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string file = e.Item.DataItem as string;
HyperLink hyp = e.Item.FindControl("hyp") as HyperLink;
hyp.Text = file;
hyp.NavigateUrl = string.Format("~/Handlers/FileHandler.ashx?file={0}", file);
FileInfo f = new FileInfo(file);
FileStream s = f.Open(FileMode.OpenOrCreate, FileAccess.Read);
}
}
public void ProcessRequest(HttpContext context)
{
//Track your id
//string id = context.Request.QueryString["id"];
context.Response.Clear();
context.Response.Buffer = true;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + context.Request.QueryString["files"]);
context.Response.WriteFile(context.Request.QueryString["files"]);
context.Response.End();
}
public bool IsReusable
{
get { return false; }
}
这是索引页面代码:
<li class='has-sub'><a href='#'><span>Safety, QA & Security</span></a>
<ul>
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
<ItemTemplate>
<asp:HyperLink ID="hyp" runat="server" target="_blank" a href="/Handlers/FileHandler.ashx?id=7"/>
<%-- another method of listing all files in specific folder --%>
<%--<% foreach(var file in Directory.GetFiles("C:\\temp\\Safety&Security", "*.*", SearchOption.AllDirectories)) { %>
<li><%= file.Substring(file.LastIndexOf(("\\"))+1) %></li>
<% } %> --%>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
答案 0 :(得分:2)
你犯了一个愚蠢的错误!
hyp.NavigateUrl = string.Format("~/Handlers/FileHandler.ashx?file={0}", file);
context.Response.WriteFile(context.Request.QueryString["files"]);
您将查询字符串设置为&#39; file&#39;但是将查询字符串作为&#39;文件&#39;哪个不存在。尝试
context.Response.WriteFile(context.Request.QueryString["file"]);
编辑:
试试这段代码。首先在c:\ temp中添加一个名为test.txt的文件并在其中放入一些文本。在你的aspx中:
<li class='has-sub'><a href='/Handlers/FileHandler.ashx?file=c:\temp\test.txt'><span>Safety, QA & Security</span></a></li>
在你的经纪人中:
public void ProcessRequest(HttpContext context)
{
//Track your id
//string id = context.Request.QueryString["id"];
context.Response.Clear();
context.Response.Buffer = true;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + context.Request.QueryString["file"]);
context.Response.WriteFile(context.Request.QueryString["file"]);
context.Response.End();
}
public bool IsReusable
{
get { return false; }
}
在处理程序中设置断点,并确保在调试时遇到处理程序。由于我在评论中提到的关于数据绑定不起作用的其他问题但是你应该至少在你的处理程序中获取查询字符串并且文本文件将被下载。这个代码不会像你需要的那样工作。