WebClient类--上传文件

时间:2022-03-08 17:43:28

 #region 把本地文件上传到指定uri
2         /// <summary>
3         /// 把本地文件上传到指定uri
4         /// </summary>
5         /// <param name="sender"></param>
6         /// <param name="e"></param>
7         private void button2_Click(object sender, EventArgs e)
8         {
9             WebClient wc = new WebClient();
10             string targetPath = "http://127.0.0.1/rss/Data Configuration.zip";
11             string sourcePath = "d:\\Data Configuration.zip";
12             this.label1.Text = string.Format("uploading {0} to {1}", targetPath, sourcePath);
13             byte[] bt = wc.UploadFile(targetPath, "PUT", sourcePath);
14             MessageBox.Show("OK");
15         }
16         #endregion
17
18
19         #region 把数据缓冲区上载到指定资源
20         /// <summary>
21         /// 把数据缓冲区上载到指定资源
22         /// </summary>
23         /// <param name="sender"></param>
24         /// <param name="e"></param>
25         private void button3_Click(object sender, EventArgs e)
26         {
27             WebClient wc = new WebClient();
28             string targetPath = "http://127.0.0.1/rss/kaifeng.jpg";
29             string sourcePath = @"C:\test.jpg";
30             FileStream fs = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);
31             byte[] bt = new byte[fs.Length];
32             fs.Read(bt, 0, bt.Length);
33             wc.UploadData(targetPath, "PUT", bt);
34         }
35         #endregion