C # File Upload class

Source: Internet
Author: User
Using system;

Namespace upfile
{
/// <Summary>
/// Summary of upfile.
/// </Summary>
Public class upfile
{
Private string Path = NULL;
Private string filetype = NULL;
Private int sizes = 0;
/// <Summary>
/// Initialize the variable
/// </Summary>
Public upfile ()
{
Path = @ "\ uploadimages \"; // upload path
FileType = "jpg | gif | bmp ";
Sizes = 200; // file size, kb by default
}

/// <Summary>
/// Set the upload path, for example, uploadimages \
/// </Summary>
Public String path
{
Set
{
Path = @ "\" + value + @"\";
}
}

/// <Summary>
/// Set the size of the uploaded file, in KB
/// </Summary>
Public int Sizes
{
Set
{
Sizes = value * 1024;
}
}

/// <Summary>
/// Set the type of the uploaded file, for example, jpg | gif | bmp
/// </Summary>
Public string FileType
{
Set
{
FileType = value;
}
}

/// <Summary>
/// Upload an image
/// </Summary>
/// <Param name = "name"> upload Control name </param>
/// <Param name = "creatDirectory"> true: the folder is created at the current time, and false is the set folder. </param>
/// <Returns> returns the relative path of the uploaded image </returns>
Public string fileSaveAs (System. Web. UI. HtmlControls. HtmlInputFile name, bool creatDirectory)
{
Try
{
String filePath = null;
// Modify the image name or 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 ();
// Obtain the physical path of the site
String uploadFilePath = null;
// If true, the folder is created at the current time; otherwise, the folder is set.
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;
}
// Obtain the File Upload path
String sourcePath = name. Value. Trim ();
// Determine whether the uploaded file is empty
If (sourcePath = "" | sourcePath = null)
{
Message ("You have not uploaded the data. Is it wrong! ");
Return NULL;
}
// Obtain the file extension
String tfiletype = sourcepath. substring (sourcepath. lastindexof (".") + 1 );
// Obtain the size of the uploaded file
Long strlen = Name. postedfile. contentlength;
// Break down the formats of files that can be uploaded
String [] temp = filetype. Split ('| ');
// Set whether the uploaded file is in the allowed format
Bool flag = false;
// Determine the size of the uploaded file
If (strlen> = sizes)
{

Message ("the uploaded image cannot be greater than" + sizes + "kb ");
Return NULL;
}
// Determine whether the uploaded file is in the allowed format
Foreach (string data in temp)
{
If (data = tFileType)
{
Flag = true;
Break;
}
}
// Upload is not allowed if it is true or false.
If (! Flag)
{
Message ("currently supported formats:" + fileType );
Return null;
}
System. Io. directoryinfo dir = new system. Io. directoryinfo (uploadfilepath );
// Check whether the folder exists. If the folder does not exist, create it.
If (! Dir. exists)
{
Dir. Create ();
}
FilePath = uploadFilePath + modifyFileName + "." + tFileType;
Name. PostedFile. SaveAs (filePath );
FilePath = path + modifyFileName + "." + tFileType;

Return filePath;

}
Catch
{
// Exception
Message ("Unknown error! ");
Return null;
}
}

Private void message (string msg, string url)
{
System. web. httpContext. current. response. write ("<script language = javascript> alert ('" + msg + "'); window. location = '"+ url +"' </script> ");
}

Private void message (string MSG)
{
System. Web. httpcontext. Current. response. Write ("<script language = JavaScript> alert ('" + MSG + "'); </SCRIPT> ");
}
}
}

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.