ASHX 处理程序引发值不能为空错误

本文关键字:不能 错误 处理 程序 ASHX | 更新日期: 2023-09-27 18:36:06

我想将特定文件夹中的文件列表显示为超链接,当用户单击链接时,它将打开相应的文件以提示用户下载或查看,但不断出现错误:

{"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>

ASHX 处理程序引发值不能为空错误

你犯了一个愚蠢的错误!

hyp.NavigateUrl = string.Format("~/Handlers/FileHandler.ashx?file={0}", file);
context.Response.WriteFile(context.Request.QueryString["files"]);
您将查询字符串

设置为"file",但将查询字符串读取为不存在的"文件"。尝试

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; }
}
在处理程序中

设置断点,并确保在调试时命中处理程序。由于我在注释中提到的有关数据绑定不起作用的另一个问题,此代码将无法正常工作,因此此代码将无法正常工作,但您至少应该在处理程序中获取查询字符串,并且文本文件将下载。

相关文章: