【问题标题】:ASHX handler throwing value cannot be null errorASHX 处理程序抛出值不能为空错误
【发布时间】:2015-01-05 01:33:53
【问题描述】:

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

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

【问题讨论】:

  • 您可以通过将所有 If (!Page.IsPostback) { .... } 分组在一个 if 语句中来清理您的 Page_Load,而不是三个不同的语句

标签: c# ashx filehandler


【解决方案1】:

你犯了一个愚蠢的错误!

hyp.NavigateUrl = string.Format("~/Handlers/FileHandler.ashx?file={0}", file);

context.Response.WriteFile(context.Request.QueryString["files"]);

您将查询字符串设置为“文件”,但将查询字符串读取为不存在的“文件”。试试

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; }
}

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

【讨论】:

  • 当您将鼠标悬停在文件上时,它的链接是什么样的?如果在抛出错误的行上设置断点,context.Request 中是什么?
  • 当我悬停其中一个文件的超链接时,它会显示“/Handlers/FileHandler.ashx?id=7”
  • context.Request 是空的..?
  • 首先要修复的是数据绑定。您的所有文件链接都是相同的,查询字符串为 id=7。它从不将查询字符串设置为 file=blah。但首先,只是为了测试,将 "context.Response.WriteFile(context.Request.QueryString["files"])" 更改为 "context.Response.WriteFile(context.Request.QueryString["id"])" 看看是否你得到 id 查询字符串参数。
  • 它在同一行返回相同的错误:/ 到底出了什么问题?天哪
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 2015-12-08
  • 1970-01-01
相关资源
最近更新 更多