Copy codeThe Code is as follows:
# Region upload files to databases and servers
Public void FN_UpFiles ()
{
// Traverse the File form Element
HttpFileCollection files = HttpContext. Current. Request. Files;
Try
{
For (int iFile = 0; iFile <files. Count; iFile ++)
{
// Check the file extension name
HttpPostedFile postedFile = files [iFile];
String fileName = ""; // defines the file name.
// String fileExtension = "";
FileName = Path. GetFileName (postedFile. FileName); // obtain the complete name of the uploaded file, that is, the file name + suffix.
Int index = fileName. IndexOf (".");
String FileType = fileName. Substring (index). ToLower (); // The suffix of the truncated file.
// FileTypeImg = "../FileTypeimg/" + hz + ". gif ";
Guid fileGuid = Guid. NewGuid (); // generate a new file name named after GUID to prevent the file name from being the same
String NewFileName = fileGuid. ToString (); // new file name
NewFileName = NewFileName + FileType; // new file name + suffix
If (postedFile. ContentLength> 2097151*1024) // determines whether the size of the uploaded file is greater than the size of the configuration file.
{
Page. RegisterStartupScript ("prompt", "<script language = 'javascript '> alert ('Sorry, your upload resources are too large! '); Return; </script> ");
Return;
}
Else
{
If (fileName! = "") // If the file name is not blank
{
Try
{
// File Virtual Path
String strpath = System. Web. HttpContext. Current. Server. MapPath ("~ /Upload/") + NewFileName;
Try
{
NRModel. File model = new NRModel. File ();
NRBLL. File bf = new NRBLL. File ();
Guid guid1 = Guid. NewGuid ();
Guid guid2 = new Guid (FolderId );
Guid guid3 = Guid. NewGuid ();
Guid guid4 = Guid. NewGuid ();
Model. Fileid = guid1;
Model. Folderid = guid2;
Model. Filepath = strpath;
Model. FileNam = fileName;
Model. FileSize = postedFile. ContentLength;
Model. Decription = TextArea1.Value. ToString ();
Model. CreateOn = DateTime. Now;
Model. CreateBy = guid3;
Model. ModefyBy = guid4;
If (bf. FN_AddNewRes (model)> 0)
{
NR. Error. Log. LogType ("Upload resource" + fileName + "success! "+" Server path: "+ strpath );
// Save the file to the specified directory (virtual directory)
PostedFile. SaveAs (System. Web. HttpContext. Current. Server. MapPath ("~ /Upload/") + NewFileName );
// Page. RegisterStartupScript ("prompt", "<script language = 'javascript '> alert ('upload successful! '); Self. opener. location. reload (); window. close (); </script> ");
AlertMsg ("Upload successful! ");
}
}
Catch (Exception ex)
{
NR. Error. Log. LogType (ex. ToString ());
}
}
Catch (Exception ex)
{
NR. Error. Log. LogType (ex. ToString ());
}
}
Else
{
Response. Write ("the file to be uploaded cannot be blank! ");
NR. Error. Log. LogType ("the file cannot be blank! ");
}
}
}
}
Catch (System. Exception ex)
{
NR. Error. Log. LogType (ex. ToString ());
}
}
# Endregion