Uploading of asp.net-html images

Source: Internet
Author: User

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= "{&quot;maxlength&quot;:300,&quot;required&quot;: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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.