0

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;
            }
        }
    }
}
Community
  • 1
  • 1
TruMan1
  • 33,665
  • 59
  • 184
  • 335
  • `Request.Files` shouldn't be empty. If you make a tiny HTML page with just a file upload, and have it send the file to your handler, is `Request.Files` *still* empty? – bzlm Sep 23 '11 at 17:14
  • 1
    To make this question useful for future visitors, please explain why the accepted answer solved your problem. :) – bzlm Sep 25 '11 at 11:50

1 Answers1

0

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • According to the question, "Request.Files is empty". – bzlm Sep 23 '11 at 16:53
  • @bzlm: Ah, sorry - that bit wasn't there when I started answering. – Jon Skeet Sep 23 '11 at 16:54
  • Wireshark sounds like overkill. Surely [Fiddler as a reverse proxy](http://www.fiddler2.com/fiddler/help/reverseproxy.asp) would be easier on the eyes. :) – bzlm Sep 23 '11 at 17:13
  • @bzlm: That's assuming that the behaviour won't change at all when a proxy is inserted into the mix. I prefer the "non-interventionist" approach of Wireshark - it's really not that hard to see what's going on with Follow Stream... – Jon Skeet Sep 23 '11 at 17:37
  • Actually, a *reverse* proxy introduces *less* heisenbugs than a normal forward proxy, since the client is unaware and doesn't adapt. Whatever reaches Fiddler in this case is what would have reached the HTTP handler otherwise, right? – bzlm Sep 23 '11 at 19:18
  • @bzlm: I hadn't noticed that you specified a reverse proxy - but I'd still rather not have *any* change to the behaviour, just invisible packet capturing. – Jon Skeet Sep 23 '11 at 19:21