asp. net method to get the path in HTML form File

  • 2020-11-30 08:14:31
  • OfStack


#region  Upload files to databases and servers  
public void FN_UpFiles() 
{ 
// traverse File The form elements  
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 = "";// Define filename  
//string fileExtension = ""; 
fileName = Path.GetFileName(postedFile.FileName);// Gets the full name of the uploaded file   The file name + suffix  
int index = fileName.IndexOf("."); 
string FileType = fileName.Substring(index).ToLower();// Intercept file suffix name  
//FileTypeImg = "../FileTypeimg/" + hz + ".gif"; 
Guid fileGuid = Guid.NewGuid();// Generate a new file name   In order to GUID Naming prevents filenames from being the same  
string NewFileName = fileGuid.ToString();// New filename  
NewFileName = NewFileName + FileType;// New filename + suffix  
if (postedFile.ContentLength > 2097151 * 1024)// Determines if it is larger than the uploaded file size in the configuration file  
{ 
Page.RegisterStartupScript(" prompt ", "<script language='javascript'>alert(' I'm sorry your upload resources are too large !');return;</script>"); 
return; 
} 
else 
{ 
if (fileName != "")// If the file name 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(" Uploading resources " + fileName + " successful !" + " 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(' Uploaded successfully !');self.opener.location.reload();window.close();</script>"); 
AlertMsg(" Uploaded successfully !"); 
} 
} 
catch (Exception ex) 
{ 
NR.Error.Log.LogType(ex.ToString()); 
} 


} 
catch (Exception ex) 
{ 
NR.Error.Log.LogType(ex.ToString()); 
} 
} 
else 
{ 
Response.Write(" The upload file cannot be empty !"); 
NR.Error.Log.LogType(" The file cannot be empty !"); 
} 
} 


} 
} 
catch (System.Exception ex) 
{ 
NR.Error.Log.LogType(ex.ToString()); 
} 
} 
#endregion 

Related articles: