C # WinForm uploading pictures, file-to-server methods Uploader.ashx

Source: Internet
Author: User

There are many online programs, at first, because the net is not very familiar with the FTP implementation is good, but later found that if the machine in the domain control, there will be problems.

A year later, ASP. NET is also familiar with, know Ajax should use ASHX, verification code also with ASHX, of course, here to say WinForm upload should also be ashx, haha, first provide a simple idea:

The ASP. Net of the received file is: UPLOADER.ASHX, related code:

View Plaincopy to Clipboardprint?
  1. <%@ WebHandler language="C #" class="Uploader"%>
  2. Using System;
  3. Using System.IO;
  4. Using System.Web;
  5. Public class Uploader:ihttphandler
  6. {
  7. public void ProcessRequest (HttpContext hc)
  8. {
  9. foreach (string filekey in HC. Request.Files)
  10. {
  11. Httppostedfile file = hc. Request.files[filekey];
  12. File. SaveAs (Path.Combine (HC). Server.MapPath ("."), file.   FileName));
  13. }
  14. }
  15. public bool IsReusable
  16. {
  17. get { return true;}
  18. }
  19. }

Send WinForm.cs related code for a picture or file:

View Plaincopy to Clipboardprint?
    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, finished, so operation, no longer do not have to control the domain control, as long as the internet can be uploaded. It's convenient enough.

If you want to bulk upload, and upload after the save in which directory and other operations can refer to LIUYONGFA (yongfa365) ' blog written:

The ASP. Net of the received file is: UPLOADER.ASHX, related code:

View Plaincopy to Clipboardprint?
  1. <%@ WebHandler language="C #" class="Uploader"%>
  2. Using System;
  3. Using System.IO;
  4. Using System.Web;
  5. Public class Uploader:ihttphandler
  6. {
  7. public void ProcessRequest (HttpContext hc)
  8. {
  9. string nowpath = Path.Combine (hc. Server.MapPath ("."), HC.   request["path"]);
  10. if (! Directory.Exists (Nowpath))
  11. {
  12. Directory.CreateDirectory (Nowpath);
  13. }
  14. foreach (string filekey in HC. Request.Files)
  15. {
  16. Httppostedfile file = hc. Request.files[filekey];
  17. string FilePath = 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. public bool IsReusable
  33. {
  34. get { return true;}
  35. }
  36. }

Send WinForm.cs related code for a picture or file:

View Plaincopy to Clipboardprint?
    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. }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.