如何从控制台应用程序上传文件到ASP.NET MVC [英] How to upload a file to ASP.NET MVC from a console application

查看:338
本文介绍了如何从控制台应用程序上传文件到ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个控制台应用程序发送一个XML文件到ASP.NET MVC 3中开发的Web应用程序,并接收另一个XML作为响应。
$ b

控制台应用程序返回的错误是:
$ b


远程服务器返回一个错误:(500)内部服务器错误。

当我运行Fiddler2时,发现这个错误:


对象引用未设置为对象的实例。


控制台应用程序中的代码是:

  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();

MVC控制器中的代码是:

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

$ / code> $ / pre>

RunTest()因为这个方法工作,当我通过表单上传文件(在一个名字相同的方法,使用方法GET)。 RunTest()返回XML与响应。



当我调试MVC应用程序时,变量文件为空!



如何修复?在我的控制台应用程序中,为了实际发送文件,需要更改哪些内容?或者是改变MVC方法?

而且,在尝试使用 WebClient 之前,我试过了这里的代码: http://msdn.microsoft.com/en-us/library/您的问题是WebClient.UploadFile不是发布的。使用名为file的输入将MVC设置为multipart / form-data的表单映射到的表单。尝试改变你的服务器端的方法:

pre $
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);
}


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.

解决方案

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

这篇关于如何从控制台应用程序上传文件到ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆