1, file Upload there is a very deadly file, if uploading a virus file, may cause your system to crash
So it's very important to judge the type of file.
Cannot be separated from the suffix name, the following shows a WebForm type of upload file case.
1, writing HTML language
<HTML> <Head></Head> <Body> <Liclass= "L-fieldcontainer"Fieldindex= "+"> <ul> <Listyle= "width:120px;text-align:right;">* Business License:</Li> <LiID= "Mainform|14"style= "Width:100px;text-align:left;"> <Divclass= "L-text"style= "WIDTH:98PX;"> <inputname= "Companylicense"Validate= "{"maxlength":300,"required":true}"class= "L-text-field Upper"style= "WIDTH:94PX;"ligeruiid= "Companylicense"ID= "Companylicense"type= "text" /> </Div></Li> <Li><imgsrc="#"Heigth= "50px"ID= "Updatepic"width= "60px" /></Li> <Li> <formID= "Editmenuform"enctype= "Multipart/form-data"> <inputname= "Menupic"ID= "Menupic"type= "File" /> <inputname= "Btnupload"ID= "Btnupload"value= "Upload"style= "width:60px"type= "button" /> <labelstyle= "Color:red">Support png,jpg format picture, size can not exceed 2M, must be clearly visible</label> <label></label> </form></Li> <Listyle= "WIDTH:3PX;"><spanclass= "L-star">*</span></Li> </ul></Li> </Body></HTML>
Writing Javasecript
Ajaxsubmit js files are from:
//Upload Image$ (' #btnUpload '). Click (function () { if($ ("#MenuPic"). Val ()) {//commits the form to the background asynchronously. $ ("#EditMenuForm"). Ajaxsubmit ({error:function(Error) {alert (error);}, URL:' Handler/validate.ashx ',/*set the page to which post is submitted*/Type:"Post",/*set up a form to submit as a POST method*/DataType:"Text",/*Set the return value type to text*/data: {"Action": "PIC"}, Success:function(data) {if(data = = "-1") {alert ("image size cannot exceed 2M");return; } if(data = = "-2") {alert ("image format is not correct, only supports images in png,jpg format");return;} $("#MenuIcon"). val (data); $("#updatePic"). attr ("src"), data); Liger.get ("Companylicense"). SetValue (data) $ ("#MenuPic"). Val (""); $("#UpdateInfo"). Text ("Upload successful! "); } }); } Else{$.messager.alert ("Error message", "Please select a legitimate picture!" "); } })
Writing Background Management system
voidValidatepic (HttpContext context) {//Upload Image Try { varRequestFile = context. request.files["Menupic"]; stringFName ="/lib/uploads/"+ Guid.NewGuid (). ToString () +System.IO.Path.GetExtension (requestfile.filename); stringPath =context. Request.mappath (fname); Requestfile.saveas (path); #regionVerifying picturesFileStream FS=NewFileStream (Path, FileMode.Open, FileAccess.Read); if(FS. Length >1024x768*1024x768*4) {context. Response.Write ("-1"); File.delete (path); //Deleting Files return; } BinaryReader Reader=NewBinaryReader (FS); stringFileclass; bytebuffer; byte[] B =New byte[2]; Buffer=Reader. ReadByte (); b[0] =buffer; Fileclass=buffer. ToString (); Buffer=Reader. ReadByte (); b[1] =buffer; Fileclass+=buffer. ToString (); Reader. Close (); Fs. Close (); #endregion if(Fileclass = ="255216"|| Fileclass = ="7173"|| Fileclass = ="13780") { //255216 is jpg;7173 is gif;6677 is bmp,13780 is png;7790 is Exe,13780:png//Response.Write ("Picture available"); //Save to databasecontext. Response.Write (fname); } Else{context. Response.Write ("-2"); File.delete (path); //Deleting Files return; } } Catch(Exception e) {context. Response.Write ("-2"); Logmanager.writelog ("Error Uploading picture! "+e.message); } }
Uploading of asp.net-html images