網(wǎng)上有很多方案,起初用時,因為對asp.net不太了解,覺得FTP實現(xiàn)不錯,可是后來發(fā)現(xiàn),如果機器在域控下,就會有問題。
一年過去了,asp.net也熟悉了,知道ajax沒事應該用ashx,驗證碼也用ashx,當然這里要說的WinForm上傳也應該是ashx了吧,哈哈,先提供簡單思路:
接收文件的asp.net是:Uploader.ashx,相關(guān)代碼:
- <%@ 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; }
}
}
發(fā)送圖片或文件的WinForm.cs 相關(guān)代碼:
- 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,完了,這樣操作后,再也不用管是不是在域控內(nèi)了,只要能上網(wǎng),就能上傳。夠方便吧。
如果你要批量上傳,還有上傳后保存在哪個目錄等操作可以參考寫的:
接收文件的asp.net是:Uploader.ashx,相關(guān)代碼:
- <%@ 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; }
}
}
發(fā)送圖片或文件的WinForm.cs 相關(guān)代碼:
- 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);
- }
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
posted on 2010-03-22 15:57
becket_zheng 閱讀(2863)
評論(0) 編輯 收藏 所屬分類:
C#