从 Webhandler.ashx 文件中的 .aspx.cs 获取变量值
Get value of variable from .aspx.cs in Webhandler.ashx file
我正在尝试接受要通过 httphandler 上传的文件,我需要来自 .aspx 中下拉列表的路径。我在 aspx.cs 文件中将路径作为变量,但我无法在 .ashx 文件中访问它。我认为它与 web.config 文件有关 我将 .ashx 的引用添加到 system.web 配置但没有更改。
'<%@ 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;
}
}
}
我试图传递来自 Jquery 的路径,但在 post 中传递 2 种数据类型时遇到了问题。
这是来自 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();
}
更简单的方法是在 HttpHandler 中启用 Session 并从那里获取最后选择的路径。
在您的网络表单后面的代码中保存会话中的路径
protected void ListDrop_SelectedIndexChanged(object sender, EventArgs e)
{
string fullFileName = Path.Combine("~/Uploads/", listDrop.SelectedItem.Text);
Session["fullFileName"] = fullFileName;
}
在您的 HttpHander 中添加 IRequiresSessionState 并从 Session 中检索路径。
public class WebHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
var fullFileName = context.Session["fullFileName"];
}
}
选定的值将对客户端可用,直到会话过期。默认情况下是 20 分钟没有收到任何新请求。然而这个时间可以增加changing configuration。此外,您可以在代码中检测到 Session 已过期,因为 context.Session["fullFileName"]
会 return null.
我正在尝试接受要通过 httphandler 上传的文件,我需要来自 .aspx 中下拉列表的路径。我在 aspx.cs 文件中将路径作为变量,但我无法在 .ashx 文件中访问它。我认为它与 web.config 文件有关 我将 .ashx 的引用添加到 system.web 配置但没有更改。
'<%@ 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;
}
}
}
我试图传递来自 Jquery 的路径,但在 post 中传递 2 种数据类型时遇到了问题。
这是来自 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();
}
更简单的方法是在 HttpHandler 中启用 Session 并从那里获取最后选择的路径。
在您的网络表单后面的代码中保存会话中的路径
protected void ListDrop_SelectedIndexChanged(object sender, EventArgs e)
{
string fullFileName = Path.Combine("~/Uploads/", listDrop.SelectedItem.Text);
Session["fullFileName"] = fullFileName;
}
在您的 HttpHander 中添加 IRequiresSessionState 并从 Session 中检索路径。
public class WebHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
var fullFileName = context.Session["fullFileName"];
}
}
选定的值将对客户端可用,直到会话过期。默认情况下是 20 分钟没有收到任何新请求。然而这个时间可以增加changing configuration。此外,您可以在代码中检测到 Session 已过期,因为 context.Session["fullFileName"]
会 return null.