zoukankan      html  css  js  c++  java
  • C# winform 上传文件到服务器

    1.首先要在服务器端新建一个网站axpx页

    然后再网站的后台写代码获取winform传过来的文件名。

    声明:这个方法虽然最简单最省事,但是上传大文件可能会报错,我的机器是10M,

    超过10M就会提示报错。

    [c-sharp] view plaincopyprint?
     
    1. //这是网站的后台代码,获取winform传过来的文件名  
    2. protected void Page_Load(object sender, EventArgs e)  
    3.  {  
    4.      foreach (string f in Request.Files.AllKeys)  
    5.      {  
    6.          HttpPostedFile file = Request.Files[f];  
    7.          file.SaveAs(@"d:/" + file.FileName);  
    8.      }  
    9.  }  

    2.至于winform那边,就只是要调用一下WebClient的UploadFile方法了。

    WebClient 属于 using System.Net; 空间下。

    [c-sharp] view plaincopyprint?
     
    1. public bool uploadFileByHttp(string webUrl,string localFileName)  
    2.         {  
    3.             // 检查文件是否存在  
    4.             if (!System.IO.File.Exists(localFileName))   
    5.             {  
    6.                 MessageBox.Show("{0} does not exist!", localFileName);  
    7.                 return false;  
    8.             }  
    9.             try  
    10.             {  
    11.                 System.Net.WebClient myWebClient = new System.Net.WebClient();  
    12.                 myWebClient.UploadFile(webUrl, "POST", localFileName);  
    13.             }  
    14.             catch  
    15.             {                 
    16.                 return false;  
    17.             }  
    18.             return true;  
    19.         }  
    20.   
    21. //调用方法属于远程服务器的地址,和保存文件的地址  
    22. this.uploadFileByHttp(" http://localhost:1878/UploadFileWebSite/UploadFile.aspx", @"D:/1.txt"); 
  • 相关阅读:
    01-helloworld
    F2. Nearest Beautiful Number (hard version) (思维+分类讨论+枚举)
    CF1559 D2. Mocha and Diana (Hard Version)
    牛客小白月赛36——全部施工完毕
    [P4735] 最大异或和——可持久化trie + 思维
    CF1322B Present(思维 + 位运算 + 双指针 + 枚举)
    牛客每日一题SCI2005扫雷
    一些没见过的dp模型
    思维训练——CF1304E 1-Trees and Queries
    思维训练——CF1292B Aroma's Search
  • 原文地址:https://www.cnblogs.com/soundcode/p/4174410.html
Copyright © 2011-2022 走看看