首页  ·  知识 ·  编程语言
WebClient上传下载用法
scottckt  博客园   .NET  编辑:dezai   图片来源:网络
具体用法如下。 我们先在IIS网站中建立一个文件夹,此处为quot;Mp3quot;,并设置此文件夹相关读写权限。 n

具体用法如下。 我们先在IIS网站中建立一个文件夹,此处为"Mp3",并设置此文件夹相关读写权限。
 

 

例1:使用WebClient中的UploadFile方法上传文件。代码如下。使用此方法需要将上传的文件夹权限设置为IIS来宾账户允许读写。
 

const string UploadFilePath = "http://localhost/Mp3/";
/// <summary>
/// 上传文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpload_Click(object sender, EventArgs e)
{
try
{
WebClient client
= new WebClient();
string uploadFilePath = UploadFilePath + DateTime.Now.ToString("yyMMddHHmmssff") + ".txt";
/*
* PUT:如果文件名在客户端确定,就使用PUT
* POST:如果文件名在服务器端确定,就使用POST
*/
client.UploadFile(uploadFilePath,
"PUT", txbFile.Text);
}
catch (Exception ex)
{
throw ex;
}
}

 

例2:使用WebClient的UpLoadData上传文件

/// <summary>
/// 上传文件 使用UploadData
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{
string uploadFilePath = UploadTestFilePath + DateTime.Now.ToString("yyMMddHHmmssff") + ".txt";
WebClient client
= new WebClient();
FileStream fStream
= new FileStream(txbFile.Text, FileMode.Open, FileAccess.Read);
BinaryReader bReader
= new BinaryReader(fStream);
byte[] uploadByte = bReader.ReadBytes(Convert.ToInt32(fStream.Length));

try
{
byte[] responseArray = client.UploadData(uploadFilePath, "PUT", uploadByte);
string returnValue = Encoding.GetEncoding("gb2312").GetString(responseArray);
}
catch (Exception ex)
{

throw ex;
}
finally
{

fStream.Close();
fStream.Dispose();
client.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
}

 

例3:使用WebClient的Write方法上传。
 

/// <summary>
/// 上传(Stream)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
try
{
string uploadFilePath = UploadTestFilePath + DateTime.Now.ToString("yyMMddHHmmssff") + ".txt";
WebClient client
= new WebClient();
client.UseDefaultCredentials
= true;
client.Credentials
= CredentialCache.DefaultCredentials;
FileStream fStream
= new FileStream(txbFile.Text, FileMode.Open, FileAccess.Read);
BinaryReader bReader
= new BinaryReader(fStream);
byte[] uploadByte = bReader.ReadBytes(Convert.ToInt32(fStream.Length));
Stream uploadStream
= client.OpenWrite(uploadFilePath, "PUT");
try
{
if (uploadStream.CanWrite)
{
uploadStream.Write(uploadByte,
0, uploadByte.Length);
}
}
catch (Exception ex)
{

throw ex;
}
finally
{

fStream.Close();
fStream.Dispose();
uploadStream.Close();
uploadStream.Dispose();
client.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
}

 

例4:打开文件,不需要验证。此处使用的是匿名账户。
 

/// <summary>
/// 打开文件 无授权
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpen_Click(object sender, EventArgs e)
{
string serverFilePath = "http://localhost/Mp3/11060715414724.txt";
WebClient client
= new WebClient();
Stream stream
= client.OpenRead(serverFilePath);
StreamReader reader
= new StreamReader(stream);
rtbData.Text
= reader.ReadToEnd();
}

 

例5:使用本地账户验证。

此处需要将IIS网站授权勾选成“集成widdows验证”。
 

/// <summary>
/// 自定义授权
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
WebClient client
= new WebClient();
NetworkCredential credential
= new NetworkCredential(@"ckt", "ckt");
client.Credentials
=
credential;
string serverFilePath = "http://localhost/Mp3/11060715414724.txt";
Stream stream
= client.OpenRead(serverFilePath);
StreamReader reader
= new StreamReader(stream);
rtbData.Text
= reader.ReadToEnd();
}

 

例6:下载文件

/// <summary>
/// 下载文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDownload_Click(object sender, EventArgs e)
{
try
{
WebClient client
= new WebClient();
string serverFile = "http://localhost/Mp3/11060715414724.txt";
WebRequest request
= WebRequest.Create(serverFile);
client.DownloadFile(serverFile,
@"d:\11.txt");
}
catch (Exception ex)
{
throw ex;
}
}

 

附:

测试程序代码: /Files/scottckt/WebClientStudy.rar
 

测试程序界面如下。
 

本文作者:scottckt 来源:博客园
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读