HTML code: (the key is to set the Enctype= "Multipart/form-data" in the form so that when the form is submitted, the file is transmitted as a binary stream to the server)
One
<formID= "Form1"Action= "Test.aspx"Method= "POST"enctype= "Multipart/form-data"> <Div> <inputtype= "File"name= "FL" /> <inputtype= "Submit"name= "SB"value= "Submit" /> </Div> </form>
Public Partial classtest:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { if(!IsPostBack) { for(inti =0; i < Request.Files.Count; i++) {Httppostedfile PostedFile=Request.files[i]; if(Postedfile.contentlength >0) { stringFileName =Postedfile.filename; stringStrexprentfile = filename.substring (Filename.lastindexof (".") +1); stringSfilepath = Server.MapPath ("~/") + FileName;//Server.MapPath ("~/123." + strexprentfile);Postedfile.saveas (Sfilepath); } Else { //This . Labmessage.text = "Cannot upload empty file";}} Response.Write ("1"); } }}
Two
<formID= "Form1"runat= "Server"Method= "POST"enctype= "Multipart/form-data"> <Div> <inputtype= "File" /> <Asp:buttonID= "Btnupload"runat= "Server"Text= "Start upload"onclick= "Btnupload_click" /> </Div> </form>
protected voidBtnupload_click (Objectsender, EventArgs e) { //Int intcount = requestclass.getformint ("Hdcount",-1);Httpfilecollection Files =HttpContext.Current.Request.Files; for(inti =0; i < Files.count; i++) {Httppostedfile PostedFile=Files[i]; if(Postedfile.contentlength >0) { stringFileName =Postedfile.filename; stringStrexprentfile = filename.substring (Filename.lastindexof (".") +1); stringSfilepath ="/uploadfile/hotel/"+ stringclass.makefilename24 () + i +Strexprentfile; Postedfile.saveas (Server.MapPath (Sfilepath)); } Else { //This . Labmessage.text = "Cannot upload empty file"; } } }
HTML <input type= "file" > Upload file--a file upload example with ASP.