相信用ASP.NET寫一個上傳文件的網頁,大家都會寫,但是有沒有人想過通過在WinForm中通過HTTP協議上傳文件呢?
有些人說要向服務器端上傳文件,用FTP協議不是很簡單嗎?效率又高,為什么還要使用HTTP協議那么麻煩呢?這里面有幾個原因:
(1)FTP服務器的部署相對麻煩,還要設置權限,權限設置不對,還會惹來一系列的安全問題。
(2)如果雙方都還有防火牆,又不想開發FTP相關的一些端口時,HTTP就會大派用場,就像WEB Services能穿透防火牆一樣。
(3)其他的...,還在想呢...
但是使用HTTP也有他的一些問題,例如不能斷點續傳,大文件上傳很難,速度很慢,所以HTTP協議上傳的文件大小不應該太大。
說了這么多,原歸正傳,一般來說,在Winform里通過HTTP上傳文件有幾種可選的方法:
(1)前面提到的Web Services,就是一種很好的方法,通過編寫一個WebMethod,包含有 byte[] 類型的參數,然后調用Web Services的方法,文件內容就會以Base64編碼傳到服務器上,然后重新保存即可。
[WebMethod] public void UploadFile(byte[] content,string filename){ Stream sw = new StreamWriter(...); sw.Close(); }
當然,這種通過Base64編碼的方法效率比較低,那么可以采用WSE,支持附件,並以2進制形式傳送,效率會更高。
(2)除了通過WebService,另外一種更簡單的方法就是通過WebClient或者HttpWebRequest來模擬HTTP的POST動作來實現。這時候首先需要編寫一個asp.net web form來響應上傳,代碼如下:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="UploadFileWeb.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body> <form id="Form1" method="post" runat="server"> </form> </body> </html>
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace UploadFileWeb { /// <summary> /// WebForm1 的摘要說明。 /// </summary> public class WebForm1 : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { // 在此處放置用戶代碼以初始化頁面 foreach( string f in Request.Files.AllKeys) { HttpPostedFile file = Request.Files[f]; file.SaveAs(@"D:\Temp\" + file.FileName); } if( Request.Params["testKey"] != null ) { Response.Write(Request.Params["testKey"]); } } #region Web 窗體設計器生成的代碼 override protected void OnInit(EventArgs e) { // // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }
其實這個頁面跟我們平常寫的asp.net上傳文件代碼是一樣的,在Web 頁的Request對象中包含有Files這個對象,里面就包含了通過POST方式上傳的所有文件的信息,這時所需要做的就是調用 Request.Files[i].SaveAs方法。
但是怎么讓才能在WinForm里面模擬想Web Form POST 數據呢?System.Net命名空間里面提供了兩個非常有用的類,一個是WebClient,另外一個是HttpWebRequest類。如果我們不需要通過代理服務器來上傳文件,那么非常簡單,只需要簡單的調用WebClient.UploadFile方法就能實現上傳文件:
private void button1_Click(object sender, System.EventArgs e)
{
WebClient myWebClient = new WebClient();
myWebClient.UploadFile("http://localhost/UploadFileWeb/WebForm1.aspx","POST",@"D:\xx.exe");
}
是不是覺得很簡單呢?確實就這么簡單。
方法二:
工作中用到winform上傳文件(-_-!,很少用winform,搞了半天)
碰到一點問題,解決如下
1、501 為實現錯誤
解決方法:
先把IISWEB服務擴展中的WebDev打開
然后
IIS站點添加MIME txt類型 常見的MIME類型如下
超文本標記語言文本 .html,.html text/html
普通文本 .txt text/plain
RTF文本 .rtf application/rtf
GIF圖形 .gif image/gif
JPEG圖形 .ipeg,.jpg image/jpeg
au聲音文件 .au audio/basic
MIDI音樂文件 mid,.midi audio/midi,audio/x-midi
RealAudio音樂文件 .ra, .ram audio/x-pn-realaudio
MPEG文件 .mpg,.mpeg video/mpeg
AVI文件 .avi video/x-msvideo
GZIP文件 .gz application/x-gzip
TAR文件 .tar application/x-tar
再然后
設置目標文件夾的可寫性
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; namespace Common { /// <summary> /// winform形式的文件傳輸類 /// </summary> public class WinFileTransporter { /// <summary> /// WebClient上傳文件至服務器,默認不自動改名 /// </summary> /// <param name="fileNamePath">文件名,全路徑格式</param> /// <param name="uriString">服務器文件夾路徑</param> public void UpLoadFile(string fileNamePath, string uriString) { UpLoadFile(fileNamePath, uriString, false); } /// <summary> /// WebClient上傳文件至服務器 /// </summary> /// <param name="fileNamePath">文件名,全路徑格式</param> /// <param name="uriString">服務器文件夾路徑</param> /// <param name="IsAutoRename">是否自動按照時間重命名</param> public void UpLoadFile(string fileNamePath, string uriString, bool IsAutoRename) { string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1); string NewFileName = fileName; if (IsAutoRename) { NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf(".")); } string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1); if (uriString.EndsWith("/") == false) uriString = uriString + "/"; uriString = uriString + NewFileName; Utility.LogWriter log = new Utility.LogWriter(); //log.AddLog(uriString, "Log"); //log.AddLog(fileNamePath, "Log"); /**/ /// 創建WebClient實例 WebClient myWebClient = new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; // 要上傳的文件 FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); //FileStream fs = OpenFile(); BinaryReader r = new BinaryReader(fs); byte[] postArray = r.ReadBytes((int)fs.Length); Stream postStream = myWebClient.OpenWrite(uriString, "PUT"); try { //使用UploadFile方法可以用下面的格式 //myWebClient.UploadFile(uriString,"PUT",fileNamePath); if (postStream.CanWrite) { postStream.Write(postArray, 0, postArray.Length); postStream.Close(); fs.Dispose(); log.AddLog("上傳日志文件成功!", "Log"); } else { postStream.Close(); fs.Dispose(); log.AddLog("上傳日志文件失敗,文件不可寫!", "Log"); } } catch (Exception err) { postStream.Close(); fs.Dispose(); //Utility.LogWriter log = new Utility.LogWriter(); log.AddLog(err, "上傳日志文件異常!", "Log"); throw err; } finally { postStream.Close(); fs.Dispose(); } } /**/ /// <summary> /// 下載服務器文件至客戶端 /// </summary> /// <param name="URL">被下載的文件地址,絕對路徑</param> /// <param name="Dir">另存放的目錄</param> public void Download(string URL, string Dir) { WebClient client = new WebClient(); string fileName = URL.Substring(URL.LastIndexOf("\\") + 1); //被下載的文件名 string Path = Dir + fileName; //另存為的絕對路徑+文件名 Utility.LogWriter log = new Utility.LogWriter(); try { WebRequest myre = WebRequest.Create(URL); } catch (Exception err) { //MessageBox.Show(exp.Message,"Error"); log.AddLog(err, "下載日志文件異常!", "Log"); } try { client.DownloadFile(URL, fileName); FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); byte[] mbyte = r.ReadBytes((int)fs.Length); FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write); fstr.Write(mbyte, 0, (int)fs.Length); fstr.Close(); } catch (Exception err) { //MessageBox.Show(exp.Message,"Error"); log.AddLog(err, "下載日志文件異常!", "Log"); } } } }
