Collection of asp.net file upload class source code _ Basic Application
Last Update:2017-01-18
Source: Internet
Author: User
Namespace WMJ
{
public class Myupload
{
Private System.Web.HttpPostedFile Postedfile=null;
private string Savepath= "";
private string extension= "";
private int filelength=0;
Display parameter information used by this component
public string Help
{
get{
String helpstring;
helpstring= "<font size=3>myupload myupload=new myupload (); Constructor ";
helpstring+= "Myupload.postedfile=file1. postedfile;//set the file to upload ";
helpstring+= "myupload.savepath=\" e:\\\ ";/set the path to upload to the server, default c:\\";
helpstring+= "MYUPLOAD.FILELENGTH=100; Set the maximum length of the uploaded file, Unit K, default 1k ";
helpstring+= "myupload.extension=\" doc\ ", set the extension of uploaded file, default txt";
helpstring+= "Label1. Text=myupload.upload ()//start uploading, and display upload results </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 to "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! ';
}
}
}
Compiled into DLLs with Csc/target:library Wmj.cs for multiple calls later
Call Example
<% @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>