您的位置:首页 > 其它

使用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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: