C# WinForm 上传图片,文件到服务器的方法Uploader.ashx
2010-12-08 18:19
741 查看
网上有很多方案,起初用时,因为对asp.net不太了解,觉得FTP实现不错,可是后来发现,如果机器在域控下,就会有问题。
一年过去了,asp.net也熟悉了,知道ajax没事应该用ashx,验证码也用ashx,当然这里要说的WinForm上传也应该是ashx了吧,哈哈,先提供简单思路:
接收文件的asp.net是:Uploader.ashx,相关代码:
view plaincopy to clipboardprint?
<%@ 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 相关代码:
view plaincopy to clipboardprint?
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,相关代码:
view plaincopy to clipboardprint?
<%@ 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 相关代码:
view plaincopy to clipboardprint?
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);
}
一年过去了,asp.net也熟悉了,知道ajax没事应该用ashx,验证码也用ashx,当然这里要说的WinForm上传也应该是ashx了吧,哈哈,先提供简单思路:
接收文件的asp.net是:Uploader.ashx,相关代码:
view plaincopy to clipboardprint?
<%@ 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; }
}
}
<%@ 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 相关代码:
view plaincopy to clipboardprint?
System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.UploadFile("http://www.yongfa365.com/Uploader.ashx", "POST", "C://WINDOWS//system32//cmd.exe");
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,相关代码:
view plaincopy to clipboardprint?
<%@ 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; }
}
}
<%@ 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 相关代码:
view plaincopy to clipboardprint?
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);
}
相关文章推荐
- C# WinForm 上传图片,文件到服务器的方法Uploader.ashx
- C# WinForm 上传图片,文件到服务器的方法Uploader.ashx
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- javascript+C#本地大文件上传到服务器方法(WebUploader)
- C# 向服务器上传文件(客服端winform、服务端web)
- winForm 图片base64编码转换上传文件到web服务器
- IIS发布网站部分特殊配置--图片上传至别的服务器、iis配置 通配符应用程序映、iis配置 C#底层拦截.htm文件请求
- jquery上传插件fineuploader上传文件使用方法(jquery图片上传插件)
- C# WinForm 通过URL取得服务器上的某图片文件到本地
- C# winform 上传文件、图片
- C# Winform 上传本地文件到服务器路径
- C# WinForm 上传文件到服务器
- android上传图片文件至C#服务器
- C# 向服务器上传文件(客服端winform、服务端web)
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- IIS发布网站部分特殊配置--图片上传至别的服务器、iis配置 通配符应用程序映、iis配置 C#底层拦截.htm文件请求
- C# winform 上传文件到服务器
- C#winform上传文件到服务器
- Java上传文件图片到服务器的方法
- C# WinForm 通过URL取得服务器上的某图片文件到本地