asp.net ways to get paths in HTML form file

Source: Internet
Author: User
Tags empty file size html form httpcontext save file tostring

  This article mainly introduces how asp.net gets the path in HTML form file, and the friends you need can refer to the following

  Code as follows: #region upload files to database and server   public void Fn_upfiles ()   { //Traverse File table cell element   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 & nbsp String fileextension = "" ";  filename = path.getfilename (postedfile.filename);//Get the full name of the uploaded file-file name + 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 to prevent file name same   string newfilename = fi Leguid.tostring ()///new filename   NewFileName = newfilename + filetype;//new filename + suffix name   if (postedfile.contentlength > 2097151 * 1024)//determine if larger than the upload 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 &nbs P 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 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    
Related Article

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.