WebClient 上传下载用法
时间:2011-06-07 来源:宽田
今天学习了一下WebClient的上传、下载。
具体用法如下。 我们先在IIS网站中建立一个文件夹,此处为"Mp3",并设置此文件夹相关读写权限。
例1:使用WebClient中的UploadFile方法上传文件。代码如下。使用此方法需要将上传的文件夹权限设置为IIS来宾账户允许读写。
/// <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方法上传。
/// 上传(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>
/// <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>
/// <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
测试程序界面如下。
相关阅读 更多 +