ASP. net2.0 uploads images and creates corresponding folders for users (original)

Source: Internet
Author: User
// Front-endCode
<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Test. aspx. CS " Inherits = " Test "   %>

<! Doctype HTML public " -// W3C // dtd xhtml 1.0 transitional // en "   " Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< HTML xmlns = " Http://www.w3.org/1999/xhtml "   >
< Head runat = " Server " >
< Title > No title page </ Title >

</ Head >
< Body >

< Form ID = " Form1 " Runat = " Server " >
< Div >
< ASP: fileupload ID = " Fileupload1 " Runat = " Server " Cssclass = " Input3 " Width = " 290px " /> & Nbsp; & Nbsp; & Nbsp; & Nbsp;
< ASP: button ID = " Button1 " Runat = " Server " Text = " Upload images and create folders " Onclick = " Button#click "   />
</ Div >
</ Form >
</ Body >
</ Html >

// Background code

Using System;
Using System. Data;
Using System. configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using System. IO;


Public Partial Class Test: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{

}
Protected   Void Button#click ( Object Sender, eventargs E)
{
String Uploadfilename, uploadfilelastname;
Uploadfilename = Fileupload1.postedfile. filename; // Original Local Location of the file
Uploadfilelastname = Uploadfilename. substring (uploadfilename. lastindexof ( " . " ) +   1 ); // Get the file extension
Random Rd =   New Random (); // Generate random number
Int Valationno =   10   + Rd. Next ( 99 ); // Generate random number
String Suiji = Valationno. tostring (); // Generate random number
String Uploadfiletime = Datetime. Now. tostring ( " Yyyymmddhhmmss " ) + Suiji; // Obtain the system time and add a random number to generate the name of the uploaded image.
String Newuploadfilename = Uploadfiletime +   " . "   + Uploadfilelastname; // Name of the uploaded Image

String Userdirectory =   " Testfile " ; // The name of the folder to be created. You can register an ID in actual use.
String Userpath = Server. mappath ( " Uploadfiles " ). Tostring () +   " \\ "   + Userdirectory +   " \\ "   +   " Productsimages " ;
If ( ! Directory. exists (userpath )) // Create a folder if the folder does not exist.
{
Directory. createdirectory (userpath );
}
Fileupload1.postedfile. saveas (newuploadpahtname ); // Upload the image as saved
String Suser_productsimageurl =   " Uploadfiles/ "   + Userdirectory +   " / "   +   " Productsimages/ "   + Newuploadfilename; // Obtain the virtual path of the server Image
// Response. Write (suser_productsimageurl );
}
}

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.