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

发布于 2024-12-06 02:57:01 字数 1387 浏览 0 评论 0 原文

我构建了一种方法,该方法采用本地文件并将其发布到从 这里是第二个答案。

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

有人可以帮助我了解如何在 HttpHandler 中使用该文件进行处理吗?我尝试了以下方法,但 Request.Files 为空:

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

I have constructed a method that takes a local file and posts it to a remote site taken from the 2nd answer here.

On the remote site, I have my HttpHandler but do not know where the file bytes are so I can save it somewhere on the remote machine.

Can someone help me on how to consume that file in the HttpHandler for processing? I tried the below but Request.Files is empty:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你爱我像她 2024-12-13 02:57:01

从上下文中获取HttpRequest,并使用Files 属性来获取 HttpPostedFile 对象。然后,您可以从 HttpPostedFile.InputStream 访问数据(还有名称、长度和 MIME 类型的其他属性)。

编辑:现在问题已被编辑以表明您已经在使用 Files 属性,我强烈怀疑您正在查看错误的 HTTP 请求,或者您的方式有问题重新提出请求。我建议您使用 Wireshark 来查看网络级别发生的情况 - 这样您就可以检查您的请求是否真的< /em> 中有文件数据。

Get hold of the HttpRequest from the context, and use the Files property to get a collection of HttpPostedFile objects. You can then access the data from HttpPostedFile.InputStream (there are other properties for the name, length and MIME type).

EDIT: Now that the question's been edited to show that you're already using the Files property, I strongly suspect that you're looking at the wrong HTTP request, or that there's something wrong with how you're making the request. I suggest you use Wireshark to see what's happening at the network level - that way you can check that your request really has the file data in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文