国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

收藏的asp.net文件上傳類源碼

2020-01-18 01:45:37
字體:
供稿:網(wǎng)友
namespace Wmj 

public class MyUpload 

private System.Web.HttpPostedFile postedFile=null; 
private string savePath=""; 
private string extension=""; 
private int fileLength=0; 
//顯示該組件使用的參數(shù)信息 
public string Help 

get{ 
string helpstring; 
helpstring="<font size=3>MyUpload myUpload=new MyUpload(); //構(gòu)造函數(shù)"; 
helpstring+="myUpload.PostedFile=file1.PostedFile;//設(shè)置要上傳的文件"; 
helpstring+="myUpload.SavePath=/"e:///";//設(shè)置要上傳到服務(wù)器的路徑,默認(rèn)c://"; 
helpstring+="myUpload.FileLength=100; //設(shè)置上傳文件的最大長度,單位k,默認(rèn)1k"; 
helpstring+="myUpload.Extension=/"doc/";設(shè)置上傳文件的擴(kuò)展名,默認(rèn)txt"; 
helpstring+="label1.Text=myUpload.Upload();//開始上傳,并顯示上傳結(jié)果</font>"; 
helpstring+="<font size=3 color=red>Design By WengMingJun 2001-12-12 All Right Reserved!</font>"; 
return helpstring; 


public System.Web.HttpPostedFile PostedFile 

get 

return postedFile; 

set 

postedFile=value; 


public string SavePath 

get 

if(savePath!="") return savePath; 
return "c://"; 

set 

savePath=value; 


public int FileLength 

get 

if(fileLength!=0) return fileLength; 
return 1024; 

set 

fileLength=value*1024; 


public string Extension 

get 

if(extension!="") return extension; 
return "txt"; 

set 

extension=value; 


public string PathToName(string path) 

int pos=path.LastIndexOf("http://"); 
return path.Substring(pos+1); 

public string Upload() 

if(PostedFile!=null) 

try{ 
string fileName=PathToName(PostedFile.FileName); 
if(!fileName.EndsWith(Extension)) return "You must select "+Extension+" file!"; 
if(PostedFile.ContentLength>FileLength) return "File too big!"; 
PostedFile.SaveAs(SavePath+fileName); 
return "Upload File Successfully!"; 

catch(System.Exception exc) 
{return exc.Message;} 

return "Please select a file to upload!"; 



用csc /target:Library Wmj.cs 編譯成dll供以后多次調(diào)用 
調(diào)用舉例 
<%@page language="C#" runat="server"%> 
<%@import namespace="Wmj"%> 
<script language="C#" runat="server"> 
void Upload(object sender,EventArgs e) 

MyUpload myUpload=new MyUpload(); 
// label1.Text=myUpload.Help; 
myUpload.PostedFile=file1.PostedFile; 
myUpload.SavePath="e://"; 
myUpload.FileLength=100; 
label1.Text=myUpload.Upload(); 

</script> 
<form enctype="multipart/form-data" runat="server"> 
<input type="file" id="file1" runat="server"/> 
<asp:Button id="button1" Text="Upload" OnClick="Upload" runat="server"/> 
<asp:Label id="label1" runat="server"/> 
</form> 
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南涧| 吴川市| 绵竹市| 林西县| 邯郸县| 青冈县| 连南| 海丰县| 苍山县| 宜兰县| 宜黄县| 噶尔县| 商南县| 通海县| 株洲市| 隆德县| 冷水江市| 洱源县| 西吉县| 小金县| 高安市| 清徐县| 云安县| 双城市| 潼关县| 峨边| 罗江县| 三穗县| 尚志市| 鹰潭市| 当涂县| 岐山县| 尖扎县| 英吉沙县| 霍山县| 漳州市| 马龙县| 九龙城区| 遂溪县| 神木县| 普安县|