asp.net how to get paths in HTML form file _ Practical Tips

Source: Internet
Author: User
Tags html form httpcontext save file
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
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.