// Set the path to save the uploaded file
String strsavedir = "./uploadface /";
String strname = faceupload. postedfile. filename;
// Obtain the index of the last "." In the file name (including the path)
Int intext = strname. lastindexof (".");
// Get the file extension
String strext = strname. substring (intext );
// The file name is automatically named Based on the date and file size to ensure that the file name is not repeated.
Datetime datnow = datetime. now;
Strname = datnow. dayofyear. tostring () + faceupload. postedfile. contentlength. tostring () + strext;
String strnewname = "adv" + datnow. dayofyear. tostring () + faceupload. postedfile. contentlength. tostring () + strext;
String strpath = strsavedir + strnewname;
// File Upload type
If (strext = ". jpg" | strext = ". GIF ")
{
// Save the file to the directory you want. This is the upload directory under the IIS root directory. You can change it.
// Note: Here I use server. mappath () to retrieve the absolute directory of the current file. In Asp.net, "\" must be replaced "\"
String Path = server. mappath (strsavedir + strname );
Faceupload. postedfile. saveas (PATH );
String newpath = server. mappath (strsavedir + strnewname );
II:
Using system;
namespace upfile
{< br> ///
/// Summary of upfile.
//
public class upfile
{< br> private string Path = NULL;
private string filetype = NULL;
private int sizes = 0;
///
// initialization variable
///
Public upfile ()
{< br> Path = @ "\ uploadfile \"; // the upload path
filetype = "JPG | GIF | BMP";
sizes = 200; // The size of the uploaded file. The default value is KB.
}
/// <Summary>
/// Set the upload path, for example, uploadfile \
/// </Summary>
Public String path
{
Set
{
Path = @ "\" + value + @"\";
}
}
/// <Summary>
/// Set the size of the uploaded file, in KB
/// </Summary>
Public int sizes
{
Set
{
Sizes = value;
}
Get
{
Return sizes;
}
}
/// <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. httppostedfile 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. filename. 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. 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/1024> = 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)
{
Message (" Program The specified file storage directory does not exist! ");
Return NULL;
// You can create a folder in the following sentence, but set it to not create a folder for security purposes.
// Dir. Create ();
}
Filepath = uploadfilepath + modifyfilename + "." + tfiletype;
Name. saveas (filepath );
Filepath = path + modifyfilename + "." + tfiletype;
// Use \ directory \ file name before uploading and use:/directory/file name on the website
Filepath = filepath. Replace ('\\','/');
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> ");
}
}
}
// Used in the programCodeIs:
// Upload the hotel photo
If (hotel photo. hasfile)
{
Upfile. upfile upfilepic = new upfile. upfile ();
Upfilepic. Sizes = 4000;
Upfilepic. Path = "uploadfile ";
Upfilepic. filetype = "JPG | GIF | BMP | PNG ";
STR _ hotel photo = upfilepic. filesaveas (hotel photo. postedfile, false );
If ("" = STR _ hotel photo | null = STR _ hotel photo)
{
Return;
}
}
Else
{
STR _ hotel photo = "";
}