Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过HttpHandler使用发布的文件?_C#_Asp.net_Forms_Http Post_Httphandler - Fatal编程技术网

C# 如何通过HttpHandler使用发布的文件?

C# 如何通过HttpHandler使用发布的文件?,c#,asp.net,forms,http-post,httphandler,C#,Asp.net,Forms,Http Post,Httphandler,我构造了一个方法,它获取本地文件并将其发布到从中获取的远程站点 在远程站点上,我有我的HttpHandler,但不知道文件字节在哪里,因此我可以将其保存在远程机器上的某个位置 有人能帮助我如何使用HttpHandler中的文件进行处理吗?我尝试了以下操作,但请求。文件为空: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Collections.Sp

我构造了一个方法,它获取本地文件并将其发布到从中获取的远程站点

在远程站点上,我有我的HttpHandler,但不知道文件字节在哪里,因此我可以将其保存在远程机器上的某个位置

有人能帮助我如何使用HttpHandler中的文件进行处理吗?我尝试了以下操作,但请求。文件为空:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;

namespace Somewhere
{
    public class UploadFileHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            //VALIDATE FILES IN REQUEST
            if (context.Request.Files.Count > 0)
            {
                //HANDLE EACH FILE IN THE REQUEST
                foreach (HttpPostedFile item in context.Request.Files)
                {
                    item.SaveAs(context.Server.MapPath("~/Temp/" + item.FileName));
                    context.Response.Write("File uploaded");
                }
            }
            else
            {
                //NO FILES IN REQUEST TO HANDLE
                context.Response.Write("No file uploaded");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
从上下文中获取HttpRequest,并使用该属性获取对象集合。然后,您可以从名称、长度和MIME类型的其他属性中访问数据


编辑:现在问题已被编辑以显示您已经在使用Files属性,我强烈怀疑您正在查看错误的HTTP请求,或者您发出请求的方式有问题。我建议您使用查看网络级别发生的情况-这样您可以检查您的请求中是否确实包含文件数据。

根据问题,request.Files是空的。@bzlm:啊,对不起-我开始回答时,这一点不存在。Wireshark听起来有点过分了。肯定会更容易看人@bzlm:这是假设在混合中插入代理时,行为不会发生任何变化。我更喜欢Wireshark的不干涉主义方法——看Follow Stream到底发生了什么并不难……事实上,反向代理比普通的正向代理引入更少的Heisenbug,因为客户端不知道并且不适应。在这种情况下,任何到达Fiddler的内容都会到达HTTP处理程序,对吗?Request.Files不应该为空。如果您只上传了一个文件就创建了一个小小的HTML页面,并让它将文件发送给您的处理程序,Request.Files是否仍然为空?为了让这个问题对未来的访问者有用,请解释为什么接受的答案解决了您的问题: