内容显示页
 
类别:.Net + C# | 浏览(1481) | 2010-1-4 17:30:48

网上有很多方案,起初用时,因为对asp.net不太了解,觉得FTP实现不错,可是后来发现,如果机器在域控下,就会有问题。

一年过去了,asp.net也熟悉了,知道ajax没事应该用ashx,验证码也用ashx,当然这里要说的WinForm上传也应该是ashx了吧,哈哈,先提供简单思路:

接收文件的asp.net是:Uploader.ashx,相关代码:

<%@ WebHandler Language="C#" Class="Uploader" %>
using System;
using System.IO;
using System.Web;

public class Uploader : IHttpHandler
{
    public void ProcessRequest(HttpContext hc)
    {
        foreach (string fileKey in hc.Request.Files)
        {
            HttpPostedFile file = hc.Request.Files[fileKey];
            file.SaveAs(Path.Combine(hc.Server.MapPath("."), file.FileName));
        }
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

 发送图片或文件的WinForm.cs 相关代码:
 

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

OK,完了,这样操作后,再也不用管是不是在域控内了,只要能上网,就能上传。够方便吧。


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

接收文件的asp.net是:Uploader.ashx,相关代码:

<%@ WebHandler Language="C#" Class="Uploader" %>
using System;
using System.IO;
using System.Web;

public class Uploader : IHttpHandler
{
    public void ProcessRequest(HttpContext hc)
    {
        string NowPath = Path.Combine(hc.Server.MapPath("."), hc.Request["path"]);

        if (!Directory.Exists(NowPath))
        {
            Directory.CreateDirectory(NowPath);
        }

        foreach (string fileKey in hc.Request.Files)
        {
            HttpPostedFile file = hc.Request.Files[fileKey];
            string FilePath = Path.Combine(NowPath, file.FileName);
            if (File.Exists(FilePath))
            {
                if (Convert.ToBoolean(hc.Request["overwrite"]))
                {
                    File.Delete(FilePath);
                }
                else
                {
                    continue;
                }
            }
            file.SaveAs(FilePath);
        }
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

 发送图片或文件的WinForm.cs 相关代码:
 

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

 


引用本页地址:http://www.yongfa365.com/item/WinForm-Uploader.ashx.html
 
 
相关链接
 
网友评论:
1 Mingle - 2010-3-4 16:27:43
想知道你是如何调试ashx文件的?
 
2 匿名网友 - 2010-3-4 16:29:45
不调试,最后输出一下
 
姓名: 记住我
网址:
邮箱:
内容:
验证码:  验证码图片 看不清? 换张图试试
 
     
 
 
文章分类
 
 
.Net + C#(59)
 
 
ASP+VBS(161)
 
 
 
Linux(10)
 
 
 
web 2.0(25)
 
 
 
 
 
心程(68)
 
生活(95)
 
 
     

Power by :柳永法(yongfa365)'Blog  | 京ICP备07011491号  QQ:64049027  E-mail:64049027@qq.com yongfa365'CodePlex yongfa365'CodeGoogle

申请友情链接 要求:跟本站主题相类似正规网站,双方交换为首页位置

转载请注明来源,以便后人及时得到最新、修正、加强版!!!