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();
}
}
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();
}
}
相关文章推荐
- WinForm上传文件至服务器
- C# 向服务器上传文件(客服端winform、服务端web)
- Winform(C#)——向服务器上传文件代码
- C# WinForm 上传文件到服务器
- (总结2)WinForm中3种方式文件上传服务器:WebClient
- C# WinForm 上传图片,文件到服务器的方法Uploader.ashx
- C# WinForm 上传图片,文件到服务器的方法Uploader.ashx
- C# winform 上传文件到服务器
- Winform--文件上传到服务器
- WinForm 上传文件到服务器上
- (总结1)WinForm中文件上传服务器:WebService
- winForm base64编码转换 上传文件到web服务器
- C# Winform文件上传服务器
- WinForm 上传文件到服务器上
- 在winform实现文件上传到服务器
- C# 向服务器上传文件(客服端winform、服务端web)
- winForm 图片base64编码转换上传文件到web服务器
- C# Winform 上传本地文件到服务器路径
- C# Winform WebClient上传文件至服务器