The simplest application example of the fileupload Upload Component in Asp.net

Source: Internet
Author: User
Tags filetime

//************************************** **************************************** *************
String savepath = server. mappath ("~ /Uploadfile /");
String temp = This. fileupload. filename;
Bool fileok = false;
String newfilename = string. empty;
If (this. fileupload. hasfile)
{
String fileexception = system. Io. Path. getextension (this. fileupload. filename). tolower ();

String [] allowedexception = {". GIF", ". BMP", ". PNG", ". jpg "};
For (INT I = 0; I <allowedexception. length; I ++)
{
If (fileexception = allowedexception [I])
Fileok = true;
}
}
If (fileok) // Image Upload
{
Try
{
Newfilename = tournewshelper. getnewfilename (this. fileupload. filename );
If (file. exists (savepath + newfilename )! = False)
{
This. fileupload. saveas (savepath + newfilename );
}
}
Catch (exception ex)
{
Commonhelper. doupalert (this. Page, "Image Upload Failed! ");
}
}
Else
{
Commonhelper. doupalert (this. Page, "Only GIF, BMP, PNG, and jpg images can be uploaded ");
}

Public static string getnewfilename (string filename)
{
// Generate a new file name consisting of time + random number with the data file name
String newfilename = datetime. Now. year. tostring () + datetime. Now. Month. tostring ()
+ Datetime. Now. Day. tostring () + datetime. Now. Hour. tostring ()
+ Datetime. Now. Second. tostring () + datetime. Now. Minute. tostring ()
+ Datetime. Now. millisecond. tostring ()
// + Rand. Next (1000). tostring ()
+ Filename. substring (filename. lastindexof ("."), filename. Length-filename. lastindexof ("."));
Return newfilename;
}

//************************************** **************************************** **********
The methods to obtain the file type are as follows:
String filecontenttype = fileupload1.postedfile. contenttype;
If (filecontenttype = "image/BMP" | filecontenttype = "image/GIF" | filecontenttype = "image/pjpeg ")

**************************************** *************************************
Private void upfile ()
{
If (uploadfile. postedfile. contentlength> 0 ))
{// Obtain the original name of the uploaded file
String filename =
Uploadfile. postedfile. filename. substring (uploadfile. postedfile. filename. lastindexof ("\\"),
Uploadfile. postedfile. filename. Length-uploadfile. postedfile. filename. lastindexof ("\\"));
/// Obtain the year, month, day, minute, second, and millisecond values of the current time, and combine them into a string in string format
String filetime = datetime. Now. year. tostring () + datetime. Now. Month. tostring ()
+ Datetime. Now. Day. tostring () + datetime. Now. Hour. tostring ()
+ Datetime. Now. Second. tostring () + datetime. Now. Minute. tostring ()
+ Datetime. Now. millisecond. tostring ();
/// Add a random number and file suffix after the time string
Filename = "\" + filetime + getrandomint ()
+ Filename. substring (filename. indexof ("."), filename. Length-filename. indexof ("."));
If (file. exists (server. mappath (request. applicationpath) + "file: // upload/" + filename) = false)
{
/// Upload the file to the server hard disk
Uploadfile. postedfile. saveas (server. mappath (request. applicationpath) + "file: // upload /"
+ Filename );
}
Else
{
Response. Write ("<SCRIPT> alert (\" this file already exists. Please rename your file! \ ") </SCRIPT> ");
}
}
Else
{
Response. Write ("<SCRIPT> alert (\" file name and content cannot be blank! \ ") </SCRIPT> ");
}
}

Private string getrandomint ()
{
Random random = new random ();
Return (random. Next (10000). tostring (); // generates a random positive integer smaller than 10000
}
Bytes ----------------------------------------------------------------------------------------------------
Delete the uploaded file: file. Delete (server. mappath (request. applicationpath) + @ "file: // upload/200583016538151246.gif ");

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.