asp.net2.0 upload pictures and create appropriate folders for users

Source: Internet
Author: User
asp.net| Create | upload | upload photos

Front Code

<%@ 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 ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:fileupload id= "FileUpload1" runat= "Server" cssclass= "INPUT3" width= "290px"/>
<asp:button id= "Button1" runat= "server" text= "Upload pictures and create Folders"/>
</div>
</form>
</body>

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 Button1_Click (object sender, EventArgs e)
{
String Uploadfilename, Uploadfilelastname;
Uploadfilename = fileupload1.postedfile.filename;//The original location of the file locally
Uploadfilelastname = uploadfilename.substring (Uploadfilename.lastindexof (".") + 1);//Get file extension
Random rd = new Random ()//Generate random number
int Valationno = + Rd. Next (99);//Generate random number
String Suiji = Valationno.tostring ();//Generate random number
String uploadfiletime = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + suiji;//Get system time and add random number to generate upload picture name
String newuploadfilename = Uploadfiletime + "." + uploadfilelastname;//produces the name of the uploaded picture

String userdirectory = "Testfile"; the name of the folder you want to create, in practice, you can register the ID for the user
String userpath = Server.MapPath ("Uploadfiles"). ToString () + "\" + userdirectory + "\" + "productsimages";
if (! Directory.Exists (UserPath))//If the folder does not exist then create
{
Directory.CreateDirectory (UserPath);
}
FileUpload1.PostedFile.SaveAs (newuploadpahtname); Upload a picture with a saved method
String suser_productsimageurl = "uploadfiles/" + userdirectory + "/" + "productsimages/" + newuploadfilename;//get server-side picture of virtual Path
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.