简体   繁体   English

从Webhandler.ashx文件中的.aspx.cs获取变量的值

[英]Get value of variable from .aspx.cs in Webhandler.ashx file

I am trying to accept files for upload through the httphandler and I need the path I have coming from a drop down list in the .aspx. 我正在尝试接受通过httphandler上传的文件,并且我需要来自.aspx下拉列表的路径。 I have the path as a variable in the aspx.cs file but I can't access it in the .ashx file. 我在aspx.cs文件中将路径作为变量,但是在.ashx文件中无法访问它。 I think it has to do with the web.config file I added reference for the .ashx to the system.web configuration but no change. 我认为这与web.config文件有关,我在System.web配置中添加了.ashx的引用,但没有任何更改。

'<%@ WebHandler Language="C#" Class="FileHandler" %>'

using System;
using System.Web;

public class FileHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    if (context.Request.Files.Count > 0)
    {
        HttpFileCollection files = context.Request.Files;

        foreach(string key in files)
        {
            HttpPostedFile file = files[key];
            string fileName = context.Server.MapPath("thisIsWhereINeedThePath" + key);

            file.SaveAs(fileName);

        }
        context.Response.ContentType = "text/plain";
        context.Response.Write("Great");

    }
}

public bool IsReusable {
    get {
        return false;
    }
 }

}

I tried to pass the path from Jquery but had trouble with 2 data types being passed in the post. 我试图从Jquery传递路径,但是在帖子中传递2种数据类型时遇到麻烦。

This is from the aspx.cs file, I am trying to get the value of the listDrop.SelectedItem.Text 这是来自aspx.cs文件,我正在尝试获取listDrop.SelectedItem.Text的值

protected void ListDrop_SelectedIndexChanged(object sender, EventArgs e)
    {
        string fullFileName = Path.Combine("~/Uploads/", listDrop.SelectedItem.Text);
        string[] filePaths = Directory.GetFiles(Server.MapPath(fullFileName));
        List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
            files.Add(new ListItem(Path.GetFileName(filePath), filePath));
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
    }

The easier way to do that is enabling Session in your HttpHandler and getting the last selected path from there. 这样做的更简单方法是在HttpHandler中启用Session并从那里获取最后选择的路径。

In code behind of your webform save the path in Session 在网络表单后面的代码中,将路径保存在Session中

protected void ListDrop_SelectedIndexChanged(object sender, EventArgs e)
{
    string fullFileName = Path.Combine("~/Uploads/", listDrop.SelectedItem.Text);
    Session["fullFileName"] = fullFileName;
}

In your HttpHander add IRequiresSessionState and retrieve the path from Session. 在您的HttpHander中,添加IRequiresSessionState并从Session中检索路径。

public class WebHandler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        var fullFileName = context.Session["fullFileName"];
    }
}

Selected value will be available for a client until Session expires. 会话期满之前,所选值将对客户端可用。 By default that is 20 minutes without getting any new request. 默认情况下为20分钟,没有收到任何新请求。 However this time can be increased by changing configuration . 但是,可以通过更改配置来增加此时间。 Additionally you can detect Session has expired in your code because context.Session["fullFileName"] would return null. 此外,由于context.Session["fullFileName"]将返回null,因此您可以在代码中检测到Session已过期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM