请教:WebClient.UploadFile 的使用方法

cuckoo_7 2004-03-25 06:00:06
使用WebClient.UploadFile方法往网络服务器上上传文件时出错,错误如下,请各位大虾指点一二。谢谢。

远程服务器返回错误: (405) 不允许的方法。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Net.WebException: 远程服务器返回错误: (405) 不允许的方法。

源错误:


行 177: //Response.End();
行 178: // Upload the file to the URL using the HTTP 1.0 POST.
行 179: byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);
行 180:
行 181: // Decode and display the response.

...全文
1582 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinqiong999 2004-04-16
  • 打赏
  • 举报
回复
up
ermachao 2004-04-01
  • 打赏
  • 举报
回复
报什么样的错误?
cuckoo_7 2004-04-01
  • 打赏
  • 举报
回复
楼上的兄弟:
这些我都已经试过了,windows程序可以,但是web程序却不行。
首先声明:要求是Web程序,而不是windows程序。
后来又在我原来的程序上面添加了用户认证。用webclient.uploadData上传不提示错误,但是数据却并没有添加到目的文件中;用webclient.uploadfile仍是上传出错。
请哪位大虾帮忙解决问题。谢谢
ermachao 2004-03-31
  • 打赏
  • 举报
回复
以上例子足已解决楼主的问题了,楼主为什么迟迟不结贴呢?
ermachao 2004-03-29
  • 打赏
  • 举报
回复
MSDN中有完整的例子,后面部份就是服务器端接收的页面代码:

Example
[Visual Basic, C#, C++] The following example uploads the specified file to the specified URI using UploadFile. Any response returned by the server is displayed on the console.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of UploadFile. For other examples that might be available, see the individual overload topics.
[Visual Basic]
Console.Write(ControlChars.Cr + "Please enter the URL to post data to : ")
Dim uriString As String = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr + "Please enter the fully qualified path of the file to be uploaded to the URL")

Dim fileName As String = Console.ReadLine()
Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString)

' Upload the file to the Url using the HTTP 1.0 POST.
Dim responseArray As Byte() = myWebClient.UploadFile(uriString, "POST", fileName)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response Received.The contents of the file uploaded are: " + ControlChars.Cr + "{0}", Encoding.ASCII.GetString(responseArray))
[C#]
Console.Write("\nPlease enter the URL 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 URL");
string fileName = Console.ReadLine();

Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);
// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);

// Decode and display the response.
Console.WriteLine("\nResponse Received.The contents of the file uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));
[C++]
Console::Write(S"\nPlease enter the URL to post data to : ");
String* uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient* myWebClient = new WebClient();

Console::WriteLine(S"\nPlease enter the fully qualified path of the file to be uploaded to the URL");
String* fileName = Console::ReadLine();

Console::WriteLine(S"Uploading {0} to {1} ...", fileName, uriString);
// Upload the file to the URL using the HTTP 1.0 POST.
Byte responseArray[] = myWebClient->UploadFile(uriString, S"POST", fileName);

// Decode and display the response.
Console::WriteLine(S"\nResponse Received::The contents of the file uploaded are: \n {0}", Encoding::ASCII->GetString(responseArray));
[Visual Basic, C#, C++] The following example shows an ASP.NET page that can accept posted files and is suitable for use with the UploadFile method. The page must reside on a Web server. Its address provides the value for the address parameter of the UploadFile method.
[Visual Basic]
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="VB" runat=server>
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

Dim f As String
Dim file
For Each f In Request.Files.AllKeys
file = Request.Files(f)
file.SaveAs("c:\inetpub\test\UploadedFiles\" & file.FileName)
Next f

End Sub

</Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>
[C#]
<%@ 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>
cuckoo_7 2004-03-27
  • 打赏
  • 举报
回复
To ermachao() :
谢谢。 WebClient.UploadFile方法也需要写接收页面?请给个例子好吗?你有QQ吗?这样交流太困难了。

TO reformer(红色时代) :
谢谢。可是你的例子是在本地上传的,但是我用的是分布式系统,是要往网络服务器上上传东东的。
cuckoo_7 2004-03-26
  • 打赏
  • 举报
回复
TO CMIC:
不需要匿名传递,我用程序试过了,远程服务器的写权限是放开的。程序显示有权限,但却总是提示上传失败,是不是我上传的方法不正确呀
还有你那个例子我也试过了,错误跟以前的一样,也是上传失败。权限有。

WebClient.OpenWrite:
uriString = "http://本机IP/res/new/test.aspx";
string fileName=Server.MapPath("aa.aspx");
FileStream fs=new FileStream(fileName,FileMode.Open,FileAccess.Read);
BinaryReader r=new BinaryReader(fs);
System.Net.WebClient myWebClient =new System.Net.WebClient();
//myWebClient.Credentials = CredentialCache.DefaultCredentials;
try
{
byte[] postArray=r.ReadBytes((int)fs.Length);
Stream postStream=myWebClient.OpenWrite(uriString,"PUT");
if (postStream.CanWrite)
{
Response.Write(postArray.Length+"\n");
postStream.Write(postArray,0,postArray.Length);
Response.Write(fileName+"上传成功!");
}
else
{
Response.Write(fileName + "文件目前不可写!");
}
postStream.Close();
}
catch (WebException errMsg)
{
Response.Write("上传失败:"+errMsg.Message);
}

--------------------
WebClient.UploadFile:
String uriString = "http://本机IP/res/new/";

// Create a new WebClient instance.
System.Net.WebClient myWebClient =new System.Net.WebClient();
myWebClient.Headers.Add("Content-Type","image/gif");
myWebClient.BaseAddress=uriString;
string fileName = Server.MapPath("aa.gif");//要上传的文件名
Response.Write("Uploading "+fileName+" to "+uriString);
try
{
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);
}
catch (WebException errMsg)
{
Response.Write("上传失败:"+errMsg.Message);
}
gucs 2004-03-26
  • 打赏
  • 举报
回复
没用过
CMIC 2004-03-26
  • 打赏
  • 举报
回复
需要匿名可以权限,看:
http://dotnet.aspx.cc/ShowDetail.aspx?id=D8F961C3-CBC1-4591-143D-236B572EB89F
reformer 2004-03-26
  • 打赏
  • 举报
回复
<%@ 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>
ermachao 2004-03-26
  • 打赏
  • 举报
回复
你在客户端用了WebClient.Upload方法,却没在IIS中支持接收文件的页面,当然会出这样的错。
aiirii 2004-03-25
  • 打赏
  • 举报
回复
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧