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);
}
}