收藏的asp.net文件上传类源码

时间:2021-05-18

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>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章