LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# 編程WinForm 上傳文件到 Asp.Net 的 Web項(xiàng)目

admin
2017年3月22日 0:15 本文熱度 6143

在做WinForm時(shí),需要WinForm同Web進(jìn)行交互,有時(shí)需要進(jìn)行圖片、文件的上傳操作.

 

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.         }  

 

在按鈕點(diǎn)擊時(shí),便會(huì)觸發(fā)提交操作,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. }  
 

 

這樣,便可以將文件通過(guò)winform 上傳到web 服務(wù)器上。


該文章在 2017/3/22 0:15:30 編輯過(guò)
關(guān)鍵字查詢(xún)
相關(guān)文章
正在查詢(xún)...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專(zhuān)業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車(chē)隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類(lèi)企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷(xiāo)售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶(hù)的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲ⅴa在线va天堂va | 五月开心丁香婷婷久久看 | 先锋国产资源不卡 | 日韩精品亚洲日韩精品一区 | 亚洲码一区二区在线 | 亚洲国产精品色婷婷 |