http://www.rzrgm.cn/soundcode/p/4174410.html
http://www.rzrgm.cn/soundcode/p/4174410.html
1.首先要在服務器端新建一個網站axpx頁
然后再網站的后臺寫代碼獲取winform傳過來的文件名。
聲明:這個方法雖然最簡單最省事,但是上傳大文件可能會報錯,我的機器是10M,
超過10M就會提示報錯。
- //這是網站的后臺代碼,獲取winform傳過來的文件名
- protected void Page_Load(object sender, EventArgs e)
- {
- foreach (string f in Request.Files.AllKeys)
- {
- HttpPostedFile file = Request.Files[f];
- file.SaveAs(@"d:/" + file.FileName);
- }
- }
2.至于winform那邊,就只是要調用一下WebClient的UploadFile方法了。
WebClient 屬于 using System.Net; 空間下。
- public bool uploadFileByHttp(string webUrl,string localFileName)
- {
- // 檢查文件是否存在
- if (!System.IO.File.Exists(localFileName))
- {
- MessageBox.Show("{0} does not exist!", localFileName);
- return false;
- }
- try
- {
- System.Net.WebClient myWebClient = new System.Net.WebClient();
- myWebClient.UploadFile(webUrl, "POST", localFileName);
- }
- catch
- {
- return false;
- }
- return true;
- }
- //調用方法屬于遠程服務器的地址,和保存文件的地址
- this.uploadFileByHttp(" http://localhost:1878/UploadFileWebSite/UploadFile.aspx", @"D:/1.txt");
浙公網安備 33010602011771號