How to upload a file to ASP.NET MVC from a console application

Go To StackoverFlow.com

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.

2012-04-03 22:22
by Lucas Reis


7

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");
}
2012-04-03 22:27
by Chris
It worked! I just needed to remove the as HttpPostedFile, it wasn't compiling... Thank you very much - Lucas Reis 2012-04-03 22:40
thanks mate, helped me - cillierscharl 2012-04-24 21:13


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

2012-04-03 22:31
by Artem
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 2012-04-03 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! ;)

2012-04-05 19:16
by Lucas Reis