Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; //context. Response.Write (context. request["Cheshi1"]); if(Context. Request.Files.Count >0) { for(inti =0; I < context. Request.Files.Count; i++) {Httppostedfile file=context. Request.files[i]; //Check file type, only receive jpg,jpeg,bmp format if(file. contenttype=="Image/jpeg") { //Create a multi-level directory random save path through a string hash code. stringGUID =Guid.NewGuid (). ToString (); intRam =GUID. GetHashCode (); intDira = Ram &0xf;//0xf ' 1111 ' integer intDIRB = (Ram >>4) &0xf;//RAM right shift 4 bit intDirc = (Ram >>8) &0xf;//RAM right shift 8 bit stringPath = context. Server.MapPath (string. Format ("upload/{0}/{1}/{2}/", Dira.tostring (), dirb.tostring (), dirc.tostring ()); System.IO.Directory.CreateDirectory (path); //Create a directory. File. SaveAs (path + GUID +"_"+file. FileName); //create thumbnails and saveImage image =image.fromstream (file. InputStream); Image smallimg=NewBitmap ( $, $* Image. Height/image. Width); Graphics g=graphics.fromimage (SMALLIMG); G.drawimage (Image,0,0, Smallimg.width, smallimg.height); Smallimg.save (Path+ GUID +"_small"+file. FileName); Context. Response.Write ("""+ file. FileName +""Upload success!" \ r \ n"); } Else{context. Response.Write ("upload file Format Error! is:"+ file. FileName +"\ r \ n"); } } } Else{context. Response.Write ("upload failed! "); } }
<script type= "Text/javascript" >window.onload=function() {document.getElementById (' Add '). onclick =function () { varFile1 = document.getElementById (' file1 ')); varext =/\. [^\.] +$/. EXEC (File1.value.toLowerCase ()); if(ext = = '. JPEG ' | | ext = = '. jpg ' | | ext = = '. bmp ') { return; } Else{alert (' Upload file format is wrong! ‘); return false; } } }; </script>
<formID= "Form1"Method= "POST"Action= "Addnews.aspx"enctype= "application/x-www-form-urlencoded"><inputtype= "File"name= "Txtimage"ID= "File1"value="" /><inputID= "Add"type= "Submit"value= "Submit" />
ASP. net File Upload