使用WebClient进行上传文件
2007-10-18 01:35
344 查看
private bool UploadFile(string source,string targetUrl,NetworkCredential networkCredential)
{
FileStream streamSource;
Stream streamTarget;
WebClient client = new WebClient();
client.Credentials = networkCredential;
try
{
streamSource = File.OpenRead(source);
}
catch (Exception err)
{
return false;
}
try
{
Uri url = new Uri(targetUrl);
streamTarget = client.OpenWrite(url, "PUT");
}
catch (Exception err)
{
return false;
}
try
{
long num = 0;
int count = 0;
byte[] buffer = new byte[512];
while (num < streamSource.Length)
{
count = streamSource.Read(buffer, 0, 512);
streamTarget.Write(buffer, 0, count);
num += count;
}
streamTarget.Close();
streamSource.Close();
return true;
}
catch (Exception err)
{
return false;
}
}
{
FileStream streamSource;
Stream streamTarget;
WebClient client = new WebClient();
client.Credentials = networkCredential;
try
{
streamSource = File.OpenRead(source);
}
catch (Exception err)
{
return false;
}
try
{
Uri url = new Uri(targetUrl);
streamTarget = client.OpenWrite(url, "PUT");
}
catch (Exception err)
{
return false;
}
try
{
long num = 0;
int count = 0;
byte[] buffer = new byte[512];
while (num < streamSource.Length)
{
count = streamSource.Read(buffer, 0, 512);
streamTarget.Write(buffer, 0, count);
num += count;
}
streamTarget.Close();
streamSource.Close();
return true;
}
catch (Exception err)
{
return false;
}
}
相关文章推荐
- 使用WebClient进行上传文件 [ZT]
- 使用WebClient进行文件上传
- ASP.NET上传下载文件示例,使用webClient实现
- DiscuzNT使用Silverlight进行多文件上传(上) 推荐
- 使用struts2进行文件上传
- 通过Ajax方式上传文件(input file),使用FormData进行Ajax请求
- H5 通过Ajax方式上传文件,使用FormData进行Ajax请求
- 在android中使用HttpURLConnection进行文件上传
- SpringMVC中使用CommonsMultipartResolver进行文件上传
- 使用Loadrunner进行文件的上传及代码测试
- 使用SpringMVC进行文件上传以及与FTP服务器对接
- 使用WebClient上传文件并同时Post表单数据字段到服务端
- WCF教程--使用流Stream进行文件的上传下载
- 在 WinForm中使用 WebClient上传文件
- 通过Ajax方式上传文件,使用FormData进行Ajax请求
- struts2学习笔记(10)——使用fileupload进行文件上传
- Javascript使用SWFUpload进行多文件上传
- Servlet3.0中使用getPart进行文件上传
- 使用apache.commons.fileupload 进行文件上传
- 使用PlupLoad js插件进行文件上传案例