Asp.net class for uploading files through the htmlinputfile Control

Source: Internet
Author: User
Method 1: Determine whether the file extension is valid

 

# Regionisallowedextension
/// <Summary>
/// Whether the extension can be uploaded
/// </Summary>
/// <Paramname = "hifile"> htmlinputfile Control </param>
/// <Returns> If yes, true is returned; otherwise, false is returned. </returns>
Public static bool isallowedextension (htmlinputfile hifile)
{
String stroldfilepath = "", strextension = "";

// Indicates the extension that can be uploaded. It can be read from the configuration file.
String [] arrextension = {". GIF", ". jpg", ". jpeg", ". BMP", ". PNG "};

If (hifile. postedfile. filename! = String. Empty)
{
Stroldfilepath = hifile. postedfile. filename;
// Get the extension of the uploaded file
Strextension = stroldfilepath. substring (stroldfilepath. lastindexof ("."));
// Determine whether the extension is valid
For (INT I = 0; I <arrextension. length; I ++)
{
If (strextension. Equals (arrextension [I])
{
Return true;
}
}
}
Return false;
}
# Endregion

  Method 2: Determine whether the size of the uploaded file exceeds the maximum value

# Region isallowedlength
/// <Summary>
/// Determine whether the size of the uploaded file exceeds the maximum value
/// </Summary>
/// <Param name = "hifile"> htmlinputfile Control </param>
/// <Returns> If the maximum value is exceeded, false is returned; otherwise, true is returned. </returns>
Public static bool isallowedlength (htmlinputfile hifile)
{
// Maximum size of the file that can be uploaded. It can be stored in an XML file, measured in KB.
Int I = 20;
// If the size of the uploaded file exceeds the maximum value, flase is returned; otherwise, true is returned.
If (hifile. postedfile. contentlength> I * 1024)
{
Return false;
}
Return true;
}
# Endregion

  Method 3: obtain a unique file name
To ensure that the uploaded file will not be overwritten, We need to rename the file based on the current time to obtain the unique file name:

# Region getuniquestring
/// <Summary>
/// Get a non-repeated file name
/// </Summary>
/// <Returns> </returns>
Public static string getuniquestring ()
{
// The obtained file name is in the format of 20050922101010
Return datetime. Now. tostring ("yyyymmddhhmmss ");
}
# Endregion

  Method 4: delete an object

# Region deletefile
/// <Summary>
/// Delete a specified object
/// </Summary>
/// <Param name = "strabsolutepath"> absolute file path </param>
/// <Param name = "strfilename"> file name </param>
Public static void deletefile (string strabsolutepath, string strfilename)
{
// Determine whether there is a \ symbol at the end of the path. If not, it is automatically added.
If (strabsolutepath. lastindexof ("\") = strabsolutepath. length)
{
// Determine whether the object to be deleted exists
If (file. exists (strabsolutepath + strfilename ))
{
// Delete an object
File. Delete (strabsolutepath + strfilename );
}
}
Else
{
If (file. exists (strabsolutepath + "\" + strfilename ))
{
File. Delete (strabsolutepath + "\" + strfilename );
}
}
}
# Endregion


  Method 5: upload a file
If the extension and size of the uploaded file are valid, upload the file to the server:

# Region SaveFile
/// <Summary>
/// Upload the file and return the file name
/// </Summary>
/// <Param name = "hifile"> htmlinputfile Control </param>
/// <Param name = "strabsolutepath"> absolute path. for example, server. mappath (@ "image/upload") and server. mappath (@ "image/upload/") can be used, and \ symbols can also be used </param>
/// <Returns> the returned file name is the uploaded file name </returns>
Public static string SaveFile (htmlinputfile hifile, string strabsolutepath)
{
String stroldfilepath = "", strextension = "", strnewfilename = "";

// If the file name to be uploaded is not empty
If (hifile. postedfile. filename! = String. Empty)
{
Stroldfilepath = hifile. postedfile. filename;
// Get the extension of the uploaded file
Strextension = stroldfilepath. substring (stroldfilepath. lastindexof ("."));
// Name of the uploaded file
Strnewfilename = getuniquestring () + strextension;
// If the end of the path is a \ symbol, upload the file directly.
If (strabsolutepath. lastindexof ("\") = strabsolutepath. length)
{
Hifile. postedfile. saveas (strabsolutepath + strnewfilename );
}
Else
{
Hifile. postedfile. saveas (strabsolutepath + "\" + strnewfilename );
}
}
Return strnewfilename;
}
# Endregion

After uploading a file, this method returns the new file name to be uploaded, in order to insert the new file name to the database.

  Method 6: upload files again
When re-uploading a file, you must first Delete the previously uploaded file, then upload the new file, and overwrite the old file name in the database with the new file name to complete the re-upload, the implementation code is as follows:

# Region coverfile
/// <Summary>
/// Re-upload the file and delete the original file
/// </Summary>
/// <Param name = "fffile"> htmlinputfile Control </param>
/// <Param name = "strabsolutepath"> absolute path. for example, server. mappath (@ "image/upload") and server. mappath (@ "image/upload/") can be used, and \ symbols can also be used </param>
/// <Param name = "stroldfilename"> old file name </param>
Public static void coverfile (htmlinputfile fffile, string strabsolutepath, string stroldfilename)
{
// Get the new file name
String strnewfilename = getuniquestring ();

If (fffile. postedfile. filename! = String. Empty)
{
// Delete the old image first when the old image is not empty
If (stroldfilename! = String. Empty)
{
Deletefile (strabsolutepath, stroldfilename );
}
SaveFile (fffile, strabsolutepath );
}
}
# Endregion

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.