Asp.net: how to obtain the path in the HTML form File

Source: Internet
Author: User

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

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.