Upload file to server

xiaoxiao2021-03-06  65

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:


"; try {for (int ifile = 0 ; iFile "); strmsg.append (" Client file address: " PostedFile.FileName "
"); strmsg.append (" Upload file name: " filename "
"); strmsg.append (" Upload file extension : " FileExtension "

"); /// 'Saves to different folders according to the different names of the extension name /// Note: You may want to modify anonymous write permission for your folder. postedFile.SaveAs (System.Web.HttpContext.Current.Request.MapPath ( "images /") fileName);}} strStatus.Text = strMsg.ToString (); return true;} catch (System.Exception Ex) {strStatus .Text = ex.Message; Return False;}}

转载请注明原文地址:https://www.9cbs.com/read-89457.html

New Post(0)