public class Upfiles
{
Path of private string path = null;//file
private string fileType = null;//File Upload type
private int sizes = 0;//size
<summary>
Initialize variables
</summary>
Public Upfiles ()
{
Path = @ "Admin\uploadfile\"; Upload path @ means escape \ No error
FileType = "Jpg|gif|bmp|png|rar|zip";//the type specified in the upload file
sizes = 200; File size, default 200KB
}
<summary>
Set the upload path, such as: uploadfile\
</summary>
public string Path
{
Set
{
Path = @ "\" + value + @ "\";
}
}
<summary>
Set the upload file size in KB
</summary>
public int Sizes
{
Set
{
sizes = value;
}
Get
{
return sizes;
}
}
<summary>
Set the type of upload file, such as: jpg|gif|bmp
</summary>
public string FileType
{
Set
{
FileType = value;
}
}
<summary>
Upload image
</summary>
<param name= "name" > Upload Control name </param>
<param name= "Creatdirectory" >true the folder is created at the current time, False is the set folder </param>
<returns> returns the relative path of the uploaded image </returns>
public string FileSaveAs (System.Web.UI.WebControls.FileUpload name, bool creatdirectory)
{
Try
{
string filePath = null;
Change the name of the picture or create a folder name at the current time
String modifyfilename = DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + DateTime.Now.Day.ToString () + DateTime.Now.Hour.ToString () + DateTime.Now.Minute.ToString () + DateTime.Now.Second.ToString () + DateTime.Now.Millisecond.ToString ();
Get the physical path to the site
string uploadfilepath = null;
If true, creates a folder at the current time, otherwise sets the folder
if (creatdirectory)
{
Uploadfilepath = System.Web.HttpContext.Current.Server.MapPath (".") + @ "\" + DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + DateTime.Now.Day.ToString () + @ "\";
}
Else
{
Uploadfilepath = System.Web.HttpContext.Current.Server.MapPath (".") + path;
Uploadfilepath = System.Web.HttpContext.Current.Server.MapPath (".. /uploadfile/");
}
Get the file's upload path
String SourcePath = name. Filename.trim ();
Determine if the upload file is empty
if (SourcePath = = "" | | SourcePath = = null)
{
Message ("You did not upload data ah, is not mistaken!");
return null;
}
Get file name extension
String tfiletype = Sourcepath.substring (Sourcepath.lastindexof (".") + 1);
Get the size of the uploaded file
Long StrLen = name. filecontent.length;;/ /. ContentLength;
Decomposition allows file format to be uploaded
string[] temp = filetype.split (' | ');
Set whether the uploaded file is in the allowed format
BOOL flag = FALSE;
Determine the upload file size
if (strlen/1024 >= sizes)
{
Message ("The uploaded picture cannot be greater than" + sizes + "KB");
return null;
}
Determine if the uploaded file is in the allowed format
foreach (string data in temp)
{
if (data = = Tfiletype)
{
Flag = true;
Break
}
}
If the upload is true, the upload is not allowed for false
if (!flag)
{
Message ("The current format supported by this system is:" + fileType);
return null;
}
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo (Uploadfilepath);
Determines whether a folder exists, does not exist, or creates
if (!dir. Exists)
{
Message ("The file storage directory for program settings does not exist!") ");
return null;
The following sentence can create a folder, but for security, set to do not create a folder
Dir. Create ();
}
FilePath = Uploadfilepath + Modifyfilename + "." + Tfiletype;
Name. SaveAs (FilePath);
FilePath = path + Modifyfilename + "." + Tfiletype;
Use before uploading: \ directory \ File name after uploading, use on website:/directory/File name
FilePath = filepath.replace (' \ \ ', '/');
return filePath;
}
Catch
{
Abnormal
Message ("An unknown error has occurred! ");
return null;
}
}
public static void Message (string msg, string URL)
{
System.Web.HttpContext.Current.Response.Write ("<script language=javascript>alert ('" + msg + "); window.location= ' + URL + ' </script> ');
}
public static void Message (String msg)
{
System.Web.HttpContext.Current.Response.Write ("<script language=javascript>alert ('" + msg + "); </script > ");
}
}
ASP. NET Upload file