ASP. NET Upload file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.