开发者

How to consume posted file via HttpHandler?

开发者 https://www.devze.com 2023-04-08 13:02 出处:网络
I have constructed a method that takes a local file and posts it to a remote site taken from the 2nd answer here.

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 a开发者_运维问答re 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;
            }
        }
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消