http://www.aygfsteel.com/ebecket 返還網
          隨筆-140  評論-11  文章-131  trackbacks-0

          網上有很多方案,起初用時,因為對asp.net不太了解,覺得FTP實現不錯,可是后來發現,如果機器在域控下,就會有問題。

          一年過去了,asp.net也熟悉了,知道ajax沒事應該用ashx,驗證碼也用ashx,當然這里要說的WinForm上傳也應該是ashx了吧,哈哈,先提供簡單思路:

          接收文件的asp.net是:Uploader.ashx,相關代碼:

          1. <%@ WebHandler Language="C#" Class="Uploader" %>   
          2. using System;   
          3. using System.IO;   
          4. using System.Web;   
          5.   
          6. public class Uploader : IHttpHandler   
          7. {   
          8.     public void ProcessRequest(HttpContext hc)   
          9.     {   
          10.         foreach (string fileKey in hc.Request.Files)   
          11.         {   
          12.             HttpPostedFile file = hc.Request.Files[fileKey];   
          13.             file.SaveAs(Path.Combine(hc.Server.MapPath("."), file.FileName));   
          14.         }   
          15.     }   
          16.   
          17.     public bool IsReusable   
          18.     {   
          19.         get { return true; }   
          20.     }   
          21. }  

           發送圖片或文件的WinForm.cs 相關代碼:
           

          1. System.Net.WebClient myWebClient = new System.Net.WebClient();   
          2. myWebClient.UploadFile("http://www.yongfa365.com/Uploader.ashx""POST""C:\\WINDOWS\\system32\\cmd.exe");  

          OK,完了,這樣操作后,再也不用管是不是在域控內了,只要能上網,就能上傳。夠方便吧。


          如果你要批量上傳,還有上傳后保存在哪個目錄等操作可以參考寫的:

          接收文件的asp.net是:Uploader.ashx,相關代碼:

          1. <%@ WebHandler Language="C#" Class="Uploader" %>   
          2. using System;   
          3. using System.IO;   
          4. using System.Web;   
          5.   
          6. public class Uploader : IHttpHandler   
          7. {   
          8.     public void ProcessRequest(HttpContext hc)   
          9.     {   
          10.         string NowPath = Path.Combine(hc.Server.MapPath("."), hc.Request["path"]);   
          11.   
          12.         if (!Directory.Exists(NowPath))   
          13.         {   
          14.             Directory.CreateDirectory(NowPath);   
          15.         }   
          16.   
          17.         foreach (string fileKey in hc.Request.Files)   
          18.         {   
          19.             HttpPostedFile file = hc.Request.Files[fileKey];   
          20.             string FilePath = Path.Combine(NowPath, file.FileName);   
          21.             if (File.Exists(FilePath))   
          22.             {   
          23.                 if (Convert.ToBoolean(hc.Request["overwrite"]))   
          24.                 {   
          25.                     File.Delete(FilePath);   
          26.                 }   
          27.                 else  
          28.                 {   
          29.                     continue;   
          30.                 }   
          31.             }   
          32.             file.SaveAs(FilePath);   
          33.         }   
          34.     }   
          35.   
          36.     public bool IsReusable   
          37.     {   
          38.         get { return true; }   
          39.     }   
          40. }  

           發送圖片或文件的WinForm.cs 相關代碼:
           

          1. string url = @"http://www.yongfa365.com/Uploader.ashx?Overwrite=true&PATH=Logs\" + DateTime.Now.ToString("yyyy-MM-dd");   
          2. foreach (string file in Directory.GetFiles(item))   
          3. {   
          4.     System.Net.WebClient myWebClient = new System.Net.WebClient();   
          5.     myWebClient.UploadFile(url, "POST", file);   
          6. }  

           


          引用本頁地址:http://www.yongfa365.com/item/WinForm-Uploader.ashx.html
          posted on 2010-03-22 15:57 becket_zheng 閱讀(2861) 評論(0)  編輯  收藏 所屬分類: C#
          主站蜘蛛池模板: 蒙阴县| 长阳| 柳林县| 中阳县| 高雄县| 拉萨市| 县级市| 德钦县| 萍乡市| 茂名市| 都安| 多伦县| 南城县| 龙口市| 武义县| 太和县| 铜陵市| 许昌市| 安仁县| 彭阳县| 乌恰县| 栾川县| 台中县| 满城县| 大名县| 阿克苏市| 金乡县| 嵊泗县| 墨竹工卡县| 伽师县| 昌邑市| 呼图壁县| 合川市| 汝州市| 乐至县| 北安市| 黔江区| 威远县| 华阴市| 乌苏市| 吴旗县|