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

首頁 > 開發 > 綜合 > 正文

一個文件上傳的類

2024-07-21 02:16:33
字體:
來源:轉載
供稿:網友
namespace wmj
{
public class myupload
{
private system.web.httppostedfile postedfile=null;
private string savepath="";
private string extension="";
private int filelength=0;
//顯示該組件使用的參數信息
public string help
{
get{
string helpstring;
helpstring="<font size=3>myupload myupload=new myupload(); //構造函數";
helpstring+="myupload.postedfile=file1.postedfile;//設置要上傳的文件";
helpstring+="myupload.savepath=/"e:///";//設置要上傳到服務器的路徑,默認c://";
helpstring+="myupload.filelength=100; //設置上傳文件的最大長度,單位k,默認1k";
helpstring+="myupload.extension=/"doc/";設置上傳文件的擴展名,默認txt";
helpstring+="label1.text=myupload.upload();//開始上傳,并顯示上傳結果</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("//");
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供以后多次調用
調用舉例
<%@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>
結論:asp.net的組件支持功能很強 我們如果充分利用可以很容易的編寫出非常方便的組件可以大大的方便我們的工作


收集最實用的網頁特效代碼!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 寿宁县| 罗甸县| 政和县| 洞口县| 论坛| 清丰县| 香港| 郴州市| 宽城| 吉林省| 海南省| 兴安盟| 太仆寺旗| 稷山县| 田阳县| 双鸭山市| 邯郸县| 万州区| 禹城市| 即墨市| 蒙阴县| 南城县| 白山市| 宁晋县| 深水埗区| 弥勒县| 独山县| 九寨沟县| 新野县| 昆明市| 阳泉市| 金川县| 根河市| 易门县| 阜新市| 神木县| 长治县| 阜宁县| 德保县| 巴彦淖尔市| 寻甸|