namespaceWmj
{
publicclassMyUpload
{
privateSystem.Web.HttpPostedFilepostedFile=null;
privatestringsavePath="";
privatestringextension="";
privateintfileLength=0;
//显示该组件使用的参数信息
publicstringHelp
{
get{
stringhelpstring;
helpstring="<fontsize=3>MyUploadmyUpload=newMyUpload();//构造函数";
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+="<fontsize=3color=red>DesignByWengMingJun2001-12-12AllRightReserved!</font>";
returnhelpstring;
}
}
publicSystem.Web.HttpPostedFilePostedFile
{
get
{
returnpostedFile;
}
set
{
postedFile=value;
}
}
publicstringSavePath
{
get
{
if(savePath!="")returnsavePath;
return"c:";
}
set
{
savePath=value;
}
}
publicintFileLength
{
get
{
if(fileLength!=0)returnfileLength;
return1024;
}
set
{
fileLength=value*1024;
}
}
publicstringExtension
{
get
{
if(extension!="")returnextension;
return"txt";
}
set
{
extension=value;
}
}
publicstringPathToName(stringpath)
{
intpos=path.LastIndexOf("");
returnpath.Substring(pos+1);
}
publicstringUpload()
{
if(PostedFile!=null)
{
try{
stringfileName=PathToName(PostedFile.FileName);
if(!fileName.EndsWith(Extension))return"Youmustselect"+Extension+"file!";
if(PostedFile.ContentLength>FileLength)return"Filetoobig!";
PostedFile.SaveAs(SavePath+fileName);
return"UploadFileSuccessfully!";
}
catch(System.Exceptionexc)
{returnexc.Message;}
}
return"Pleaseselectafiletoupload!";
}
}
}
用csc/target:LibraryWmj.cs编译成dll供以后多次调用
调用举例
<%@pagelanguage="C#"runat="server"%>
<%@importnamespace="Wmj"%>
<scriptlanguage="C#"runat="server">
voidUpload(objectsender,EventArgse)
{
MyUploadmyUpload=newMyUpload();
//label1.Text=myUpload.Help;
myUpload.PostedFile=file1.PostedFile;
myUpload.SavePath="e:";
myUpload.FileLength=100;
label1.Text=myUpload.Upload();
}
</script>
<formenctype="multipart/form-data"runat="server">
<inputtype="file"id="file1"runat="server"/>
<asp:Buttonid="button1"Text="Upload"OnClick="Upload"runat="server"/>
<asp:Labelid="label1"runat="server"/>
</form>