Practical C # upload source code

Source: Internet
Author: User

///////////////// //////////
System. Text. stringbuilder strbder = new system. Text. stringbuilder (); // stringbuilder object to form a string

String fullfilename = This. file1.postedfile. filename; // obtain all the paths and file names of the selected files in the client browser.
String upfilename = fullfilename. substring (fullfilename. lastindexof ("\") + 1); // obtain the file name by taking "\" as the index. The purpose is to obtain the file format
String filetype = upfilename. substring (upfilename. lastindexof (".") + 1); // use "." As the index block to obtain the file type

// The following operations prevent multiple users from simultaneously uploading at the same time, resulting in identical file names and overwriting,
// Avoid using random numbers for a long time. You can increase the random number and continue to reduce the chance of duplicate names.
System. datetime currenttime = new system. datetime (); // creation time object
Currenttime = system. datetime. Now; // you can specify the current time.
Strbder. append (currenttime. year. tostring (); // year
Strbder. append (currenttime. Month. tostring (); // month
Strbder. append (currenttime. Day. tostring (); // day
Strbder. append (currenttime. Hour. tostring (); // hour
Strbder. append (currenttime. Minute. tostring (); // minute
Strbder. append (currenttime. Second. tostring (); // second
Random objrdm = new random (); // create a random object
Strbder. append (objrdm. Next (1,200). tostring (); // generate a random number between 1 -.

Upfilename = strbder. tostring () + "." + filetype; // The name of the file officially created on the server.
Upfilename = upfilename. tolower (); // set all to lower case to avoid inconvenience
Filetype = filetype. tolower (); // It is set to lower case, mainly used to judge

If (filetype = "BMP" | filetype = "jpg" | filetype = "GIF") // accept the type, which can shield the case sensitivity of user extensions.
{
Upfilepos = server. mappath (".. \ upload") + "\" + upfilename;
This. file1.postedfile. saveas (upfilepos); // perform the upload operation
// Response. Write ("<SCRIPT> window. Alert ('added successfully! '); </SCRIPT> "); // prompt that the user is successful.
}
Else
{
Response. Write ("<SCRIPT> window. Alert ('file format error! Only files in BMP, JPG, and GIF formats are accepted '); </SCRIPT> "); // a format error is prompted.
}
///////////////// //////////

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.