• C# WinForm 上传图片,文件到服务器的方法Uploader.ashx


    网上有很多方案,起初用时,因为对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,完了,这样操作后,再也不用管是不是在域控内了,只要能上网,就能上传。够方便吧。


    如果你要批量上传,还有上传后保存在哪个目录等操作可以参考柳永法(yongfa365)'Blog写的:

    接收文件的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. }  
  • 相关阅读:
    POJ 1637:Sightseeing tour
    bzoj 3997: [TJOI2015]组合数学
    [CEOI2008]order
    【网络流24题】星际转移问题
    Codeforces Round #460 D. Karen and Cards
    bzoj 3142: [Hnoi2013]数列
    codeforces586B
    codeforces631B
    codeforces548B
    codeforces515B
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1762664.html
Copyright © 2020-2023  润新知