使用Windows应用程序将xml文件上传到我的网页

问题描述:



我想使用Windows应用程序将XML文件从系统上传或传输到我的网页.我的网站在其他Web主机上维护.

Hi,

I want to upload or transfer XML file from my system to my webpage using windows application. My website is maintained at some other Web Hosting.

在ASP .NET MVC中,查看代码为:

In ASP .NET MVC, View code is:

<% using (Html.BeginForm("index", "home", FormMethod.Post, new { enctype = "mutipart/form-data" })) { %>
    <input type="file" name="file" />
    <input type="submit" value="Upload" />
<% } %




控制器代码为:




Controller code is:

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0 && file.ContentType == "text/xml")
    {
        var document = new XmlDocument();
        document.Load(file.InputStream);
    }
    return View();
}


要通过HTTP上传,您需要使用 ^ ]创建对目标URL的请求,将请求内容设置为文件,方法设置为POST并提交.然后,服务器端代码将接受请求并将文件内容写入服务器文件系统.

或者,您可以对ftp://URL使用相同的方法来创建 FtpWebRequest [^ ],如果您尝试通过FTP凭据上传.

在这两种情况下,您都希望包括某种身份验证,并且最好通过SSL提交请求(即,使用HTTPS进行HTTP上传或使用SFTP进行FTP提交),以使其他人无法轻易地将内容转储到您的服务器上(至少会允许DOS攻击,并且可能会发生更糟糕的事情,例如覆盖数据或寄生使用主机作为自己的站点).
To upload through HTTP, you need to use WebRequest.Create[^] to create a request to your target URL, set the request content to the file, the method to POST and submit it. Server side code would then take the request and write the file content to the server file system.

Alternatively, you can use the same method with an ftp:// URL to create a FtpWebRequest[^], if you are trying to upload through FTP credentials.

In both cases, you want to include some kind of authentication, and preferably submit the request over SSL (i.e. use HTTPS for a HTTP upload or SFTP for an FTP one), so that other people can''t trivially dump stuff on your server (at minimum that would permit DOS attacks, and probably much worse things like overwriting of data or parasitically using your host for their own sites).


Windows应用程序:

WebClient webClient =新的WebClient();
字符串logFileName ="file.xml";
字符串webAddress = null;
试试
{

webAddress = @"http://localhost:1182.com/Default.aspx";


webClient.Credentials =新的NetworkCredential("usrid","psw");

WebRequest serverRequest = WebRequest.Create(webAddress);
Web响应服务器响应;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();

webClient.UploadFile(webAddress,"POST",logFileName);
webClient.Dispose();
webClient = null;

}
捕获(异常错误)
{
//MessageBox.Show(error.Message);
}
Windows application:

WebClient webClient = new WebClient();
string logFileName = "file.xml";
string webAddress = null;
try
{

webAddress = @"http://localhost:1182.com/Default.aspx";


webClient.Credentials = new NetworkCredential("usrid", "psw");

WebRequest serverRequest = WebRequest.Create(webAddress);
WebResponse serverResponse;
serverResponse = serverRequest.GetResponse();
serverResponse.Close();

webClient.UploadFile(webAddress, "POST", logFileName);
webClient.Dispose();
webClient = null;

}
catch (Exception error)
{
//MessageBox.Show(error.Message);
}