国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# WinForm 上傳圖片,文件到服務器的方法Uploader.ashx

2019-11-17 03:00:56
字體:
來源:轉載
供稿:網友
C# WinForm 上傳圖片,文件到服務器的方法Uploader.ashx

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

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

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

view plaincopy to clipboardPRint?
  1. <%@WebHandlerLanguage="C#"Class="Uploader"%>
  2. usingSystem;
  3. usingSystem.IO;
  4. usingSystem.Web;
  5. publicclassUploader:IHttpHandler
  6. {
  7. publicvoidProcessRequest(HttpContexthc)
  8. {
  9. foreach(stringfileKeyinhc.Request.Files)
  10. {
  11. HttpPostedFilefile=hc.Request.Files[fileKey];
  12. file.SaveAs(Path.Combine(hc.Server.MapPath("."),file.FileName));
  13. }
  14. }
  15. publicboolIsReusable
  16. {
  17. get{returntrue;}
  18. }
  19. }

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

view plaincopy to clipboardprint?
  1. System.Net.WebClientmyWebClient=newSystem.Net.WebClient();
  2. myWebClient.UploadFile("http://www.yongfa365.com/Uploader.ashx","POST","C://WINDOWS//system32//cmd.exe");

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


如果你要批量上傳,還有上傳后保存在哪個目錄等操作可以參考柳永法(yongfa365)'Blog寫的:

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

view plaincopy to clipboardprint?
  1. <%@WebHandlerLanguage="C#"Class="Uploader"%>
  2. usingSystem;
  3. usingSystem.IO;
  4. usingSystem.Web;
  5. publicclassUploader:IHttpHandler
  6. {
  7. publicvoidProcessRequest(HttpContexthc)
  8. {
  9. stringNowPath=Path.Combine(hc.Server.MapPath("."),hc.Request["path"]);
  10. if(!Directory.Exists(NowPath))
  11. {
  12. Directory.CreateDirectory(NowPath);
  13. }
  14. foreach(stringfileKeyinhc.Request.Files)
  15. {
  16. HttpPostedFilefile=hc.Request.Files[fileKey];
  17. stringFilePath=Path.Combine(NowPath,file.FileName);
  18. if(File.Exists(FilePath))
  19. {
  20. if(Convert.ToBoolean(hc.Request["overwrite"]))
  21. {
  22. File.Delete(FilePath);
  23. }
  24. else
  25. {
  26. continue;
  27. }
  28. }
  29. file.SaveAs(FilePath);
  30. }
  31. }
  32. publicboolIsReusable
  33. {
  34. get{returntrue;}
  35. }
  36. }

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

view plaincopy to clipboardprint?
  1. stringurl=@"http://www.yongfa365.com/Uploader.ashx?Overwrite=true&PATH=Logs/"+DateTime.Now.ToString("yyyy-MM-dd");
  2. foreach(stringfileinDirectory.GetFiles(item))
  3. {
  4. System.Net.WebClientmyWebClient=newSystem.Net.WebClient();
  5. myWebClient.UploadFile(url,"POST",file);
  6. }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 株洲县| 普兰县| 图木舒克市| 松溪县| 谷城县| 宁南县| 吉首市| 尚志市| 娄底市| 韶关市| 甘洛县| 察哈| 兰坪| 无锡市| 长岭县| 宁晋县| 达日县| 建宁县| 高碑店市| 灵石县| 白玉县| 阳泉市| 随州市| 合江县| 米林县| 泾阳县| 剑川县| 霍州市| 大厂| 天全县| 琼结县| 星子县| 陵川县| 平武县| 腾冲县| 炎陵县| 北流市| 罗甸县| 克什克腾旗| 平顶山市| 福清市|