Copy Code code as follows:
#region uploading files to databases and servers
public void Fn_upfiles ()
{
Traversing file Form elements
httpfilecollection files = HttpContext.Current.Request.Files;
Try
{
for (int ifile = 0; ifile < files. Count; ifile++)
{
Check file extension name
Httppostedfile postedfile = Files[ifile];
string filename = "";//define FileName
String fileextension = "";
filename = Path.getfilename (postedfile.filename);//Get the full name of the uploaded file-filename + suffix name
int index = Filename.indexof (".");
String FileType = filename.substring (index). ToLower ()//intercepting file suffix name
Filetypeimg = ".. /filetypeimg/"+ Hz +". gif ";
GUID fileguid = Guid.NewGuid ();//Generate new file name with GUID name prevent file name
String newfilename = Fileguid.tostring ();//New file name
NewFileName = newfilename + filetype;//new filename + suffix name
if (Postedfile.contentlength > 2097151 * 1024)//determine if greater than the file size in the configuration file
{
Page.registerstartupscript ("Hint", "<script language= ' JavaScript" >alert (' sorry your uploading resources are too big! '); Return;</script> ");
Return
}
Else
{
if (filename!= "")//if the filename is not empty
{
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 resources" + FileName + "Success!" + "Server path:" + strpath);
Save file to specified directory (virtual directory)
Postedfile.saveas (System.Web.HttpContext.Current.Server.MapPath ("~/upload/") + NewFileName);
Page.registerstartupscript ("Hint", "<script language= ' JavaScript" >alert (' upload success! '); Self.opener.location.reload (); Window.close ();</script> ");
Alertmsg ("Upload success!");
}
}
catch (Exception ex)
{
Nr. Error.Log.LogType (ex. ToString ());
}
}
catch (Exception ex)
{
Nr. Error.Log.LogType (ex. ToString ());
}
}
Else
{
Response.Write ("Upload file cannot be empty!");
Nr. Error.Log.LogType ("File cannot be empty!");
}
}
}
}
catch (System.Exception ex)
{
Nr. Error.Log.LogType (ex. ToString ());
}
}
#endregion