LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# 编程WinForm 上传文件到 Asp.Net 的 Web项目

admin
2017年3月22日 0:15 本文热度 5190

在做WinForm时,需要WinForm同Web进行交互,有时需要进行图片、文件的上传操作.

 

WinForm的代码如下:

 

C#代码  收藏代码
  1. private void button2_Click(object sender, EventArgs e)  
  2.         {  
  3.             // string a = MyUploader(this.textBox1.Text.Trim(), @"http://localhost:4451/TEST/Default.aspx");  
  4.   
  5.             string a = MyUploader(this.textBox1.Text.Trim(), @"http://localhost/release/Default.aspx");  
  6.   
  7.           MessageBox.Show(a);  
  8.         }  
  9.   
  10.   
  11.         public static string MyUploader(string strFileToUpload, string strUrl)  
  12.         {  
  13.             string strFileFormName = "file";  
  14.             Uri oUri = new Uri(strUrl);  
  15.             string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");  
  16.   
  17.             // The trailing boundary string  
  18.             byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");  
  19.   
  20.             // The post message header  
  21.             StringBuilder sb = new StringBuilder();  
  22.             sb.Append("--");  
  23.             sb.Append(strBoundary);  
  24.             sb.Append("\r\n");  
  25.             sb.Append("Content-Disposition: form-data; name=\"");  
  26.             sb.Append(strFileFormName);  
  27.             sb.Append("\"; filename=\"");  
  28.             sb.Append(Path.GetFileName(strFileToUpload));  
  29.             sb.Append("\"");  
  30.             sb.Append("\r\n");  
  31.             sb.Append("Content-Type: ");  
  32.             sb.Append("application/octet-stream");  
  33.             sb.Append("\r\n");  
  34.             sb.Append("\r\n");  
  35.             string strPostHeader = sb.ToString();  
  36.             byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);  
  37.   
  38.             // The WebRequest  
  39.             HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri);  
  40.             oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary;  
  41.             oWebrequest.Method = "POST";  
  42.   
  43.             // This is important, otherwise the whole file will be read to memory anyway...  
  44.             oWebrequest.AllowWriteStreamBuffering = false;  
  45.   
  46.             // Get a FileStream and set the final properties of the WebRequest  
  47.             FileStream oFileStream = new FileStream(strFileToUpload, FileMode.Open, FileAccess.Read);  
  48.             long length = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length;  
  49.             oWebrequest.ContentLength = length;  
  50.             Stream oRequestStream = oWebrequest.GetRequestStream();  
  51.   
  52.             // Write the post header  
  53.             oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);  
  54.   
  55.             // Stream the file contents in small pieces (4096 bytes, max).  
  56.             byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)oFileStream.Length))];  
  57.             int bytesRead = 0;  
  58.             while ((bytesRead = oFileStream.Read(buffer, 0, buffer.Length)) != 0)  
  59.                 oRequestStream.Write(buffer, 0, bytesRead);  
  60.             oFileStream.Close();  
  61.   
  62.             // Add the trailing boundary  
  63.             oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length);  
  64.             WebResponse oWResponse = oWebrequest.GetResponse();  
  65.             Stream s = oWResponse.GetResponseStream();  
  66.             StreamReader sr = new StreamReader(s);  
  67.             String sReturnString = sr.ReadToEnd();  
  68.             Console.WriteLine(sReturnString);  
  69.             // Clean up  
  70.             oFileStream.Close();  
  71.             oRequestStream.Close();  
  72.             s.Close();  
  73.             sr.Close();  
  74.   
  75.             return sReturnString;  
  76.         }  

 

在按钮点击时,便会触发提交操作,Web的Default.aspx代码如下:

 

C#代码  收藏代码
  1. public partial class _Default : System.Web.UI.Page   
  2. {  
  3.     protected void Page_Load(object sender, EventArgs e)  
  4.     {  
  5.         if (Request.Files.Count > 0)  
  6.         {  
  7.             try  
  8.             {  
  9.                 HttpPostedFile file = Request.Files[0];  
  10.                 string filePath = this.MapPath("FileUpload") + "\\" + file.FileName;  
  11.                 file.SaveAs(filePath);  
  12.   
  13.                 Response.Write("成功了");  
  14.        
  15.             }  
  16.             catch (Exception)  
  17.             {  
  18.   
  19.                 Response.Write("失败了1");  
  20.             }  
  21.         }  
  22.         else  
  23.         {  
  24.             Response.Write("失败了2");  
  25.         }  
  26.   
  27.         Response.End();  
  28.     }  
  29. }  
 

 

这样,便可以将文件通过winform 上传到web 服务器上。


该文章在 2017/3/22 0:15:30 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved