您的位置:首页 > 其它

winform 上传文件至服务器上

2009-10-16 22:23 323 查看
前段日子,我们的一个WEB系统要做一个CS子项目。用来测试元器件数据的WINFORM程序。其中有一个就是需要上传一个测试标准文件至服务器。WINFORM上传与BS的不同。没有fileupload控件。下面是代码:

Code
private void button2_Click(object sender, EventArgs e)
{
string filename = this.textBox1.Text.ToString(); //这里是选择文件后,你的文件的位置
UpLoadFile(filename, @"\\192.168.10.10\public\public\15.测试文件"); //这个路径是服务器地址 下PUBLIC文件夹下的15.测试文件夹
}

private void UpLoadFile(string FileUrl, string ServerFile)
{
string newurl= FileUrl.Substring(FileUrl.LastIndexOf("\\")+1);
string fileext=newurl.Substring(newurl.LastIndexOf(".")+1);
if (ServerFile.EndsWith("\\") == false)
{
ServerFile = ServerFile + "\\"; //这里就是在 15.测试文件后加\\
}
ServerFile = ServerFile + newurl;
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
System.IO.FileStream fs = new System.IO.FileStream(FileUrl,System.IO.FileMode.Open,System.IO.FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] fcount = br.ReadBytes((int)fs.Length);
Stream stre = wc.OpenWrite(ServerFile,"PUT");

stre.Write(fcount, 0, fcount.Length); //通过文件流将 fcount 二进制数组 写入服务器路径
stre.Close();
fs.Dispose();
stre.Dispose();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: