private void SendFile()
{
string sUri = "http://192.168.12.100/test/aaa.xml";
WebClient myWebClient = new WebClient();
Uri uUri = new Uri(sUri); try
{
myWebClient.UploadFile(uUri, @"d:\aaa.xml");
MessageBox.Show("上传成功!");
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}报错:405 不允许的方法
下面的代码示例使用 UploadFile 将指定的文件上载到指定的 URI。由服务器返回的任何响应都显示到控制台。C# 复制代码
Console.Write("\nPlease enter the URI to post data to : ");
String uriString = Console.ReadLine();// Create a new WebClient instance.
WebClient myWebClient = new WebClient();Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URI");
string fileName = Console.ReadLine();
Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);// Upload the file to the URI.
// The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method.
byte[] responseArray = myWebClient.UploadFile(uriString,fileName);// Decode and display the response.
Console.WriteLine("\nResponse Received.The contents of the file uploaded are:\n{0}",
System.Text.Encoding.ASCII.GetString(responseArray)); 下面的代码示例显示了一个可以接受发送的文件并且适合用于 UploadFile 方法的 ASP.NET 页。该页必须驻留在 Web 服务器上。它的地址提供了 UploadFile 方法的 address 参数的值。<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%><Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
foreach(string f in Request.Files.AllKeys) {
HttpPostedFile file = Request.Files[f];
file.SaveAs("c:\\inetpub\\test\\UploadedFiles\\" + file.FileName);
}
}</Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%> <Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) { foreach(string f in Request.Files.AllKeys) {
HttpPostedFile file = Request.Files[f];
file.SaveAs("c:\\inetpub\\test\\UploadedFiles\\" + file.FileName);
}
} </Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>
我把这段代码放到网站里了...
客户端应该怎样调用这个页面呢?