C# 从Webhandler.ashx文件中的.aspx.cs获取变量的值
我正在尝试接受通过httphandler上传的文件,我需要.aspx中下拉列表中的路径。我在aspx.cs文件中将路径作为变量,但在.ashx文件中无法访问它。我认为这与web.config文件有关,我在system.web配置中添加了对.ashx的引用,但没有更改C# 从Webhandler.ashx文件中的.aspx.cs获取变量的值,c#,asp.net,webforms,generic-handler,C#,Asp.net,Webforms,Generic Handler,我正在尝试接受通过httphandler上传的文件,我需要.aspx中下拉列表中的路径。我在aspx.cs文件中将路径作为变量,但在.ashx文件中无法访问它。我认为这与web.config文件有关,我在system.web配置中添加了对.ashx的引用,但没有更改 '<%@ WebHandler Language="C#" Class="FileHandler" %>' using System; using System.Web; public class FileHandl
'<%@ 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;
}
}
}
“”
使用制度;
使用System.Web;
公共类文件处理程序:IHttpHandler{
公共void ProcessRequest(HttpContext上下文){
如果(context.Request.Files.Count>0)
{
HttpFileCollection files=context.Request.files;
foreach(文件中的字符串键)
{
HttpPostedFile file=files[key];
字符串文件名=context.Server.MapPath(“thisiswheredinedthepath”+键);
file.SaveAs(文件名);
}
context.Response.ContentType=“text/plain”;
语境。回应。写作(“很棒”);
}
}
公共布尔可重用{
得到{
返回false;
}
}
}
我试图从Jquery传递路径,但在post中传递2种数据类型时遇到问题
'<%@ 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;
}
}
}
这是来自aspx.cs文件,我正在尝试获取listDrop.SelectedItem.Text的值
'<%@ 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;
}
}
}
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();
}
protectedvoid ListDrop\u SelectedIndexChanged(对象发送方,事件参数e)
{
string fullFileName=Path.Combine(“~/Uploads/”,listDrop.SelectedItem.Text);
string[]filepath=Directory.GetFiles(Server.MapPath(fullFileName));
列表文件=新列表();
foreach(文件路径中的字符串文件路径)
{
添加(新列表项(Path.GetFileName(filePath),filePath));
}
GridView1.DataSource=文件;
GridView1.DataBind();
}
更简单的方法是在HttpHandler中启用会话并从中获取最后选择的路径
'<%@ 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;
}
}
}
在webform的代码隐藏中,在会话中保存路径
'<%@ 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;
}
}
}
protected void ListDrop_SelectedIndexChanged(object sender, EventArgs e)
{
string fullFileName = Path.Combine("~/Uploads/", listDrop.SelectedItem.Text);
Session["fullFileName"] = fullFileName;
}
在您的HttpHander中添加iRequestResessionState并从会话中检索路径
'<%@ 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;
}
}
}
public class WebHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
var fullFileName = context.Session["fullFileName"];
}
}
在会话到期之前,选定的值将可用于客户端。默认情况下,这是20分钟,不会收到任何新请求。但是,这一时间可以增加。此外,您还可以检测代码中的会话已过期,因为
上下文。会话[“fullFileName”]
将返回null。由于Web表单编译模型,您无法从其他页面或处理程序访问.aspx.cs
。即使您可以(请参阅我的答案中的解决方法),它也不会有帮助,因为您需要选定的值。因此,您必须将下拉列表中的值与文件一起发送(即使很困难)。我是否可以使用两个ajax帖子来实现这一点,然后存储一个帖子的路径,并在处理另一个帖子时使用该值?
'<%@ 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;
}
}
}