Public void uploadfile (Object sender, eventargs e) {// Check upload files are not empty if (myfile.postedfile! = null) {string nam = myfile.postedFile.FileName; // Finish file name (pose path) A "." Index int i = nam.lastindexof ("."); // Get the file extension String newext = nam.substring (i); // Here I automatically naming the file according to the date and file size, make sure File name does not repeat datetime now = datetime.now; string newname = now.dayofyear.tostring () myfile.postedfile.contentLENGTH.TOSTRING ();
/ / Save the file to the directory you want, here is an UPLOADFILES directory below IIS root //: I will use server.mappath () to take the absolute directory of the current file. In ASP.NET "/" must be used "/ / "Instead of myfile.postedfile.saveas (Server.mAppath (" newName newext)); // Get this file related properties: file name, file type, file size fname.text = myfile .PostedFile.FileName; fenc.Text = myFile.PostedFile.ContentType; fsize.Text = myFile.PostedFile.ContentLength.ToString (); Image1.ImageUrl = "http: // localhost / web / news / uploadfiles /" newname Newext;}}
HTML page code
<% @ Page language = "c #" codebehind = "Webform2.aspx.cs" autoeventwireup = "false" inherits = "Web.news.webform2"%>