黃小二的讀書筆記
有才而性緩定屬大才,有智而氣和斯為大智。人偏狹我受之以寬容,人險仄我持之以坦蕩。緩事宜急干,敏則有功;急事宜緩辦,忙則多措。 --李叔同
首頁
新隨筆
聚合
管理
隨筆-7 評論-24 文章-102 trackbacks-0
在 WinForm中使用 WebClient上傳文件
使用指定的方法將指定的本地文件上載到指定的資源
在WinForm中通過HTTP協議向服務器端上傳文件
1.服務器端處理程序
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Upload.aspx.cs
"
Inherits
=
"
Upload
"
%>
前臺代碼 Upload.aspx
using
System;
using
System.Web;
public
partial
class
Upload : System.Web.UI.Page
{
//
服務器默認保存路徑
private
readonly
string
serverPath
=
@"
C:\upload\
"
;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
//
獲取 http提交上傳的文件, 并改名保存
foreach
(
string
key
in
Request.Files.AllKeys)
{
HttpPostedFile file
=
Request.Files[key];
string
newFilename
=
DateTime.Now.ToString(
"
yyMMddhhmmssffff
"
)
+
file.FileName.Substring(file.FileName.LastIndexOf(
'
.
'
));
try
{
//
文件保存并返回相對路徑地址
file.SaveAs(
this
.serverPath
+
newFilename);
Response.Write(
"
upload/
"
+
newFilename);
}
catch
(Exception)
{
}
}
}
}
后臺代碼 Upload.aspx.cs
2.客戶端程序
/**/
///
<summary>
///
單個文件上傳至服務器
///
</summary>
///
<param name="uriAddress">
接收文件資源的URI, 例如:
http://xxxx/Upload.aspx
</param>
///
<param name="filePath">
要發送的資源文件, 例如: @"D:\workspace\WebService 相關.doc
</param>
///
<returns>
返回文件保存的相對路徑, 例如: "upload/xxxxx.jpg" 或者出錯返回 ""
</returns>
private
string
UploadFile(
string
uriAddress,
string
filePath)
{
//
利用 WebClient
System.Net.WebClient webClient
=
new
System.Net.WebClient();
webClient.Credentials
=
System.Net.CredentialCache.DefaultCredentials;
try
{
byte
[] responseArray
=
webClient.UploadFile(uriAddress,
"
POST
"
, filePath);
string
savePath
=
System.Text.Encoding.ASCII.GetString(responseArray);
return
savePath;
}
catch
(Exception)
{
return
""
;
}
}
ps.判斷遠程文件是否存在
public
bool
UriExists(
string
url)
{
try
{
new
System.Net.WebClient().OpenRead(url);
return
true
;
}
catch
(System.Net.WebException)
{
return
false
;
}
}
posted on 2009-05-26 08:57
黃小二
閱讀(5161)
評論(1)
編輯
收藏
所屬分類:
C#
評論:
#
re: 在 WinForm中使用 WebClient上傳文件
2014-11-26 17:28 |
熱熱
44444444444444444444444
回復
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
[轉] DataBinder.Eval用法
[轉]Response.Redirect(),Server.Transfer(),Server.Execute()的區別
DataTable、DataView、ListBox 數據綁定與查詢
Enterprise Library 4 之 DAAB使用
存儲過程返回的多結果集數據,ado 訪問調用
iis tomcat 服務集成
C# 程序發布/部署相關
Enterprise Library 企業庫筆記
C# XML的一點記錄
Xml 處理類(更新中...)
<
2014年11月
>
日
一
二
三
四
五
六
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
文章分類
(147)
[DB](5)
[DB].MySQL(7)
[DB].Oracle(14)
[DB].SQL Server(8)
Ajax(13)
ASP.NET(18)
C#(19)
J2EE(22)
J2SE(12)
S/S2SH(15)
Web Design(8)
雜談(6)
文章檔案
(108)
2010年6月 (1)
2010年5月 (12)
2010年4月 (18)
2009年9月 (3)
2009年8月 (2)
2009年7月 (6)
2009年6月 (3)
2009年5月 (7)
2009年4月 (10)
2009年3月 (1)
2009年1月 (1)
2008年12月 (4)
2008年11月 (1)
2008年10月 (17)
2008年9月 (17)
2008年8月 (2)
2008年7月 (3)
在線幫助
Java API Specifications
Java 開源大全
javaNB 在線文檔
MSDN 技術資源庫
MySQL 5.1參考手冊
Oracle Documentation
w3school 在線教程
開源軟件庫
Ajax/JavaScript腳本大全
Asp.net源碼專業站
CSDN開源頻道
CSS9.NET
源碼愛好者
社區
developerWorks 中國
最新評論
1.?re: SQL Server 2005/2008 對With Encryption選項創建的存儲過程解密
評論內容較長,點擊標題查看
--專業祛痘
2.?re: SQL Server 2005/2008 對With Encryption選項創建的存儲過程解密
評論內容較長,點擊標題查看
--lolola
3.?re: 在 WinForm中使用 WebClient上傳文件
44444444444444444444444
--熱熱
4.?re: 使用 HibernateTemplate 實現分頁查詢 (HibernateCallback接口)
評論內容較長,點擊標題查看
--redcoatjk
5.?re: SQL Server 2005/2008 對With Encryption選項創建的存儲過程解密
評論內容較長,點擊標題查看
--謝謝樓主
評論排行榜
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 黃小二
主站蜘蛛池模板:
黄陵县
|
大田县
|
广安市
|
晋中市
|
潜江市
|
平利县
|
阳城县
|
南岸区
|
卢龙县
|
龙游县
|
南郑县
|
柳林县
|
噶尔县
|
正镶白旗
|
额尔古纳市
|
南昌市
|
芦溪县
|
应城市
|
蒙自县
|
紫阳县
|
海淀区
|
鄂托克前旗
|
灌南县
|
冀州市
|
镇远县
|
朝阳市
|
许昌市
|
孟连
|
克拉玛依市
|
绥阳县
|
漳平市
|
承德市
|
苗栗市
|
咸宁市
|
博客
|
朔州市
|
湖北省
|
商南县
|
扶余县
|
利川市
|
黔西县
|