winform采用POST上传指定文件,并获取返回值
2011-02-15 11:00
399 查看
由于最近有个项目需要批量上传到服务器,并以一定的规则过滤上传数据。故做了个C#小程序来实现。该方法也是借鉴了网上的方法,并精简了代码。废话少说,来看代码。
Web端代码
代码
1 private void button2_Click(object sender, EventArgs e)
2 {
3
4 if (openFileDialog1.ShowDialog() == DialogResult.OK)
5 label2.Text = openFileDialog1.FileName;
6 FileInfo f = new FileInfo(openFileDialog1.FileName);
7 WebUpload(openFileDialog1.SafeFileName, f);
8
9 }
10 protected void WebUpload(string fileName, FileInfo f)
11 {
12 WebClient webc = new WebClient();
13 FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read);
14 byte[] byteString=new byte[f.Length];
15 fs.Read(byteString, 0,Convert.ToInt32(f.Length));
16 fs.Close();
17
18 byte[] returnVal = webc.UploadData("http://localhost/sz/transform.aspx?fileName="+HttpUtility.UrlEncode(fileName,Encoding.GetEncoding("gb2312")), "post",byteString);
19
20 MessageBox.Show(Encoding.GetEncoding("gb2312").GetString(returnVal));//返回值
21
22 }
Web端代码
代码
1 private void button2_Click(object sender, EventArgs e)
2 {
3
4 if (openFileDialog1.ShowDialog() == DialogResult.OK)
5 label2.Text = openFileDialog1.FileName;
6 FileInfo f = new FileInfo(openFileDialog1.FileName);
7 WebUpload(openFileDialog1.SafeFileName, f);
8
9 }
10 protected void WebUpload(string fileName, FileInfo f)
11 {
12 WebClient webc = new WebClient();
13 FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read);
14 byte[] byteString=new byte[f.Length];
15 fs.Read(byteString, 0,Convert.ToInt32(f.Length));
16 fs.Close();
17
18 byte[] returnVal = webc.UploadData("http://localhost/sz/transform.aspx?fileName="+HttpUtility.UrlEncode(fileName,Encoding.GetEncoding("gb2312")), "post",byteString);
19
20 MessageBox.Show(Encoding.GetEncoding("gb2312").GetString(returnVal));//返回值
21
22 }
相关文章推荐
- winform下通过webclient使用非流方式上传(post)数据和文件
- php采用curl实现POST数据与文件上传与接收
- iframe无刷新跨域上传文件并获取返回值
- Ajax上传文件,获取返回值,拒绝页面刷新
- httpclient (httppost)上传文件 指定格式(text/plain)
- iframe无刷新跨域上传文件并获取返回值
- [WinForm] 获取 Resources 资源文件里里指定类型的数据列表
- form文件上传、下载,获取返回值
- Django后台获取前端post上传的文件方法
- winform下通过webclient使用非流方式上传(post)数据和文件
- iframe无刷新跨域上传文件并获取返回值
- winform中进行post上传文件
- C#后台post数据到指定页面并获取返回值
- winform下通过webclient使用非流方式上传(post)数据和文件
- winform通过HttpWebRequest(post方式)上传文件和传递参数
- winform下通过webclient使用非流方式上传(post)数据和文件
- Django后台获取前端post上传的文件
- (原创)js提交文件,js上传文件,纯js解决无刷新文件上传,不使用form提交文件,并获取返回值(路径url)
- java如何获取jsp页面上传的文件路径 20 前台jsp: <form action="AA.do?method=yktAdd" method="post" > <input type='text'
- <input type="file">定义样式并获取上传文件路径及指定文件类型