7

I'm trying to have a console application to send a XML file to a web application developed in ASP.NET MVC 3, and receive another XML as a response.

The error returned in the console application is:

The remote server returned an error: (500) Internal Server Error.

When I get Fiddler2 running, I see this error:

Object reference not set to an instance of an object.

The code in the console application is:

static void Main(string[] args)
{
    var wc = new WebClient();
    byte[] response = wc.UploadFile("http://mysite.com/Tests/Test", "POST", "teste.xml");
    string s = System.Text.Encoding.ASCII.GetString(response);
    Console.WriteLine(s);
    Console.ReadKey();
}

The code in the MVC Controller is:

[HttpPost]
public ActionResult Test(HttpPostedFileBase file)
{
    XElement xml = XElement.Load(new System.IO.StreamReader(file.InputStream));
    var test = new MyTest();
    return File(test.RunTest(xml), "text/xml", "testresult.xml");
}

RunTest() works well, since this method works when I upload the file via form (in a method with the same name, using method GET). RunTest() returns the XML with the response.

When I debug the MVC application, I see the problem: the variable file is null!

How do I fix that? What do I have to change in my console application for it to actually send a file? Or is it the case to change the MVC method?

And, before trying to use WebClient, I tried this code here: http://msdn.microsoft.com/en-us/library/debx8sh9.aspx, and had the same results.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Lucas Reis
  • 743
  • 11
  • 21

3 Answers3

8

Your problem is that WebClient.UploadFile isn't posting a form with the enctype set to multipart/form-data using an input named "file" for MVC to map to. Try changing your server side method to this:

[HttpPost]
public ActionResult Test()
{
    var file = Request.Files[0] as HttpPostedFile;
    XElement xml = XElement.Load(new System.IO.StreamReader(file.InputStream));
    var test = new MyTest();
    return File(test.RunTest(xml), "text/xml", "testresult.xml");
}
Chris
  • 27,596
  • 25
  • 124
  • 225
  • 1
    It worked! I just needed to remove the `as HttpPostedFile`, it wasn't compiling... Thank you very much! – Lucas Reis Apr 03 '12 at 22:40
  • I know this question is old, but to, using the Request i get the erros that the namespace "Request" could not be found – Rubs Bieira Mar 11 '21 at 11:43
0

You need to pass name for parameter with uploaded file. This is not possible with WebClient.

Check

How to specify form parameter when using webclient to upload file

Send multipart/form-data content type request

Community
  • 1
  • 1
Artem
  • 3,700
  • 1
  • 27
  • 35
  • Nice links; by the way, the link doesn't need to work with a form, just with this request, so Chris' answer is just what I needed. But thanks anyway! =) – Lucas Reis Apr 03 '12 at 22:43
0

In case someone has the slightly different but related problem:

I also had to do it using the function UploadData instead of UploadFile. In this case, instead of writing in the controller:

var file = Request.Files[0] as HttpPostedFile;
XElement xml = XElement.Load(new System.IO.StreamReader(file.InputStream));

One can simply write:

XElement xml = XElement.Load(new System.IO.StreamReader(Request.InputStream));

Easier! ;)

Lucas Reis
  • 743
  • 11
  • 21