使用WebClient上传文件时的错误问题解决

今天在项目中使用WebClient从应用程序上传文件,应该说这是一个很简单的应用,也就调用一个UploadFile方法而已,然而在实验时却遇到了好几个错误,为此郁闷了一个上午,现在把我尝试的经过记录下来,希望对遇到这类问题的朋友有所帮助!开始我是这样写上传代码的:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 使用WebClient上传文件测试
ExpandedBlockEnd.gif
/// </summary>

None.gif public   class  WebClientTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Server  URL
InBlock.gif
        string uriString = "http://localhost/FileUpLoad/002.gif";
InBlock.gif        
InBlock.gif        
// Local Directory File Info
InBlock.gif
        string fileName = @"c:\temp\002.gif";
InBlock.gif
InBlock.gif        
// Create a new WebClient instance.
InBlock.gif
        WebClient myWebClient = new WebClient();
InBlock.gif
InBlock.gif        Console.WriteLine(
"Uploading {0} to {1} dot.gif",fileName,uriString);  
InBlock.gif                    
InBlock.gif        
// Upload the file to the URL using the HTTP 1.0 POST.
InBlock.gif
        byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);
InBlock.gif
InBlock.gif        
// Decode and display the response.
InBlock.gif
        Console.WriteLine("\nResponse Received.The contents of the file uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));
InBlock.gif        
InBlock.gif        
//Waite for User
InBlock.gif
        Console.ReadLine();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


运行后不如人愿,弹出了“远程服务器返回错误: (404) 未找到的错误对话框。开始修改方法,把POST修改为PUT

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 使用WebClient上传文件测试
ExpandedBlockEnd.gif
/// </summary>
None.gif public   class  WebClientTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Server  URL
InBlock.gif
        string uriString = "http://localhost/FileUpLoad/002.gif";
InBlock.gif        
InBlock.gif        
// Local Directory File Info
InBlock.gif
        string fileName = @"c:\temp\002.gif";
InBlock.gif
InBlock.gif        
// Create a new WebClient instance.
InBlock.gif
        WebClient myWebClient = new WebClient();
InBlock.gif
InBlock.gif        Console.WriteLine(
"Uploading {0} to {1} dot.gif",fileName,uriString);  
InBlock.gif                    
InBlock.gif        
// Upload the file to the URL using the HTTP 1.0 POST.
InBlock.gif
        byte[] responseArray = myWebClient.UploadFile(uriString,"PUT",fileName);
InBlock.gif
InBlock.gif        
// Decode and display the response.
InBlock.gif
        Console.WriteLine("\nResponse Received.The contents of the file uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));
InBlock.gif        
InBlock.gif        
//Waite for User
InBlock.gif
        Console.ReadLine();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


再运行,还是没有出现想要的提示信息,却弹出了“远程服务器返回错误: (501) 未实现”的错误。没办法,Google一把吧,可是找来找去也没有找到自己想要得解决方法,只到了这样一段话:

您可以通过如下的方法实现从win applicationupload file

假设上传目录的物理路径为c:\upload,urlhttp://localhost/upload

1.IISupload虚拟目录属性中的directory security中的anonymous access and authentication control一栏中,点击edit,选中Anonymous access,并在virtual directory一栏选中write属性。

2.c:\upload目录属性中的Security设置为everyone

3.在程序中使用如下的代码就可以实现file upload

  WebClient myclient =  new WebClient();

  myclient.UploadFile ("http://localhost/upload/odbc.ini","PUT","e:\\temp\\ODBC.INI");

——微软全球技术中心 技术支持

前面所说的这些权限我都已经设置了啊,而且跟这里所说的分毫不差,不可能微软说的也是错误的吧。现在我对自己的机器设置开始有点怀疑了。于是让同事帮我试试,同事机器上竟然上传成功了!现在问题基本上可以确定出在我的机器上,到底哪儿出问题了呢?

既然错误是从服务器上返回的,那就从服务器的IIS开始吧,先允许所有的Web服务扩展。再运行一遍,终于成功了。看来问题就出在了Web服务扩展上了,于是采用排除法,禁止一个测试一遍,这样终于确定了原来是Web服务扩展中的WebDAV惹得祸12.gif

如果你在使用WebClient上传文件的过程中遇到了“远程服务器返回错误: (501) 未实现”这样的错误,记得先把Web服务扩展中的WebDAV修改为允许。现在问题总算解决了,可以松口气了,等等……,问题又来了,我上传的图片文件,然而上传到服务器后却打不开!icon18.gif再次修改代码,这次直接以文件流上传,修改后的代码如下:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 使用WebClient上传文件测试
ExpandedBlockEnd.gif
/// </summary>
None.gif public   class  WebClientTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Server  URL
InBlock.gif
        string uriString = "http://localhost/FileUpLoad/2006327143303_Grid1.jpg";
InBlock.gif        
InBlock.gif        
// Local Directory File Info
InBlock.gif
        string fileName = @"c:\temp\2006327143303_Grid1.jpg";
InBlock.gif
InBlock.gif        
// Create a new WebClient instance.
InBlock.gif
        WebClient myWebClient = new WebClient();
InBlock.gif
InBlock.gif        FileStream fs 
= new FileStream(fileName,FileMode.Open,FileAccess.Read);
InBlock.gif
InBlock.gif        BinaryReader br 
= new BinaryReader(fs);
InBlock.gif
InBlock.gif        Byte[] postArray 
= br.ReadBytes(Convert.ToInt32(fs.Length));
InBlock.gif
InBlock.gif        Stream postStream 
= myWebClient.OpenWrite(uriString,"PUT");
InBlock.gif
InBlock.gif        
if(postStream.CanWrite)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            postStream.Write(postArray,
0,postArray.Length);
ExpandedSubBlockEnd.gif        }

InBlock.gif        postStream.Close();
InBlock.gif        fs.Close();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


这样终于可以了,上传后的图片也能打开了。可是为什么用UploadFile方法上传后的图片打不开呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值