在 WinForm中使用 WebClient上传文件


使用指定的方法将指定的本地文件上载到指定的资源

在WinForm中通过HTTP协议向服务器端上传文件


1.服务器端处理程序

None.gif <% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Upload.aspx.cs "  Inherits = " Upload "   %>

前台代码  Upload.aspx


None.gif using  System;
None.gif
using  System.Web;
None.gif
None.gif
public  partial  class  Upload : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//服务器默认保存路径
InBlock.gif
    private readonly string serverPath = @"C:\upload\";
InBlock.gif
InBlock.gif    
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{   // 获取 http提交上传的文件, 并改名保存
InBlock.gif
        foreach (string key in Request.Files.AllKeys)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            HttpPostedFile file 
= Request.Files[key];
InBlock.gif            
string newFilename = DateTime.Now.ToString("yyMMddhhmmssffff")
InBlock.gif                
+ file.FileName.Substring(file.FileName.LastIndexOf('.'));
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{   //文件保存并返回相对路径地址
InBlock.gif
                file.SaveAs(this.serverPath + newFilename);
InBlock.gif                Response.Write(
"upload/" + newFilename);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

后台代码 Upload.aspx.cs
 



2.客户端程序

ExpandedBlockStart.gif ContractedBlock.gif         /**/ /// <summary>
InBlock.gif        
/// 单个文件上传至服务器
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="uriAddress">接收文件资源的URI, 例如: http://xxxx/Upload.aspx</param>
InBlock.gif        
/// <param name="filePath">要发送的资源文件, 例如: @"D:\workspace\WebService 相关.doc</param>
ExpandedBlockEnd.gif        
/// <returns>返回文件保存的相对路径, 例如: "upload/xxxxx.jpg" 或者出错返回 ""</returns>

None.gif          private   string  UploadFile( string  uriAddress,  string  filePath)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//利用 WebClient
InBlock.gif
            System.Net.WebClient webClient = new System.Net.WebClient();
InBlock.gif            webClient.Credentials 
= System.Net.CredentialCache.DefaultCredentials;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] responseArray = webClient.UploadFile(uriAddress, "POST", filePath);
InBlock.gif                
string savePath = System.Text.Encoding.ASCII.GetString(responseArray);
InBlock.gif                
return savePath;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "";
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }




ps.判断远程文件是否存在

None.gif          public   bool  UriExists( string  url)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
new System.Net.WebClient().OpenRead(url);
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (System.Net.WebException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
 

 

转载于:https://www.cnblogs.com/kiant71/archive/2009/05/26/1752028.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值