1. Upload a single file to the server code
IF (myfile.postedfile! = null) {string nam = myfile.postedFile.fileName; // Get the last "." index INT i = nam.lastIndexof ("."); / / Get file extension string newext = nam.substring (i); // I will automatically name the file according to the date and file size, make sure the 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 UPLOAD directory in the IIS root directory. You can change. // Note: I will use server.mappath () The absolute directory of the current file. "/" In ASP.NET must be "//" instead of myfile.postedfile.saves (server.mappath ("images /") newname); // 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 ()
2, simultaneously upload multiple files to the server code
Protected system.Web.ui.WebControls.Button UploadButton; protected system.web.ui.webcontrols.label strStatus
Private void Page_load (Object Sender, System.EventArgs E) {// The user code is placed here to initialize the page if (this.ispostback) .saveImages ();
Private Boolean SaveImages () {/// 'Traversed File Form Elements HTTPFILECOLLECTION FILES = httpContext.current.Request.Files;
/// 'statin system.text.stringbuilder strmsg = new system.text.StringBuilder (); strmsg.Append ("Uploaded files are: