簡體   English   中英

ASHX處理程序的拋出值不能為空錯誤

[英]ASHX handler throwing value cannot be null error

我想將特定文件夾中的文件列表顯示為超鏈接,並且當用戶單擊鏈接時,它將打開相應的文件以提示用戶下載或查看,但是始終出現錯誤:

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

您犯了一個愚蠢的錯誤!

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

在您的處理程序中設置一個斷點,並確保在調試時處理程序被命中。 由於我在注釋中提到的另一個問題,即您的數據綁定不起作用,因此該代碼無法按您需要的方式工作,但至少應在處理程序中獲取querystring,然后文本文件將下載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM