Upload pictures without refreshing and use the C # language

Source: Internet
Author: User

Writing this is purely a memory for yourself.

The code of the foreground and the JS of the call

<! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Content-type"Content="text/html; Charset=utf-8"/> <title> uploads </title> <script src="Jquery-1.4.1.min.js"></script> <script src="Ajaxfileupload.js"></script>class="am-u-md-4"> <!--file display places-"ImgShow1"alt="" class="am-img-circle Am-img-thumbnail"Src=".. /.. /tools[no]pic.png"/> </div> <divclass="Am-form-group"> <input id="Fileuploadchange"runat="Server"Name="Browse"Onchange="ajaxFrontCoverUpload1 ();"style="width:50%"Type="file"/> <input id="Fileuploadchangeid"runat="Server"Type="Hidden"Value=""/> <!--image upload-<!--hide an input id--> <pclass="Am-form-help"> here to choose the product to upload big picture ...</p> </div></body>//upload large images asynchronouslyfunction AjaxFrontCoverUpload1 () {//detects if a picture file is being uploaded            varFilePath = $ ("#fileUploadChange"). Val (); if(FilePath! ="") {                varExtname = Filepath.substr (Filepath.lastindexof ('.') +1, Filepath.length-filepath.lastindexof ('.')); if(Extname! ="jpg"&& Extname! ="BMP"&& Extname! ="PNG"&& Extname! ="JPEG"&& Extname! ="gif") {alert ("the picture must be a file in bmp,png,jpeg,jpg,gif format! "); return; }            }            //Upload picture Operation$.ajaxfileupload ({URL:'.. /uploadimagehandler.ashx', Secureuri:false,//whether a security protocol is requiredFileelementid:'Fileuploadchange',//Upload Control IDType'POST', DataType:"text", Success:function (data) {varCode = data.substring (0,1); if(Code = ="1") {                            varFilePath = data.substring (3); FilePath= Filepath.replace ("\\","/"); $("#fileUploadChangeId"). Val (FilePath);//storing file paths in hidden fields$("#imgShow1"). attr ("src", FilePath); Alert ("Upload Success! "); } Else{alert (data); }                    }                }            )            return false; }    </script>

Two. General handling procedures

<%@ WebHandler language="C #"class="Uploadimagehandler"%>usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingsystem.web;usingSystem.Web.SessionState; Public classUploadimagehandler:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; if(Context. Request.Files.Count >0) {Httppostedfile file= Context. request.files[0]; if(file. ContentLength >0)            {                stringsuffix = file. Filename.substring (file. Filename.lastindexof ('.'));//suffix                if(!suffix. ToLower (). Contains (". bmp") &&!suffix. ToLower (). Contains (". PNG") &&!suffix. ToLower (). Contains (". JPEG") &&!suffix. ToLower (). Contains (". jpg") &&!suffix. ToLower (). Contains (". gif") {context. Response.Write ("the picture format must be in the following format: Bmp,png,jpeg,jpg,gif"); return; }                Try                {                    stringFilePath = (@"~/image/") + DateTime.Now.ToString ("YYYYMMDD") +"/"+ DateTime.Now.ToString ("hhmmssfff"); Directory.CreateDirectory (context.                    Server.MapPath (FilePath)); stringFilefullname = FilePath +"/"+file.                    FileName; File. SaveAs (context.                    Server.MapPath (Filefullname)); stringJSON ="{\ "msg\": \ ""+ Filefullname +"\ ", code:\" 10000\ "}"; Context. Response.Write ("1_"+filefullname); return; }                Catch(Exception ex) {context. Response.Write (Httputility.htmlencode (ex.                    Message)); return; }} context. Response.Write ("Please select a file to upload!"); return; } context. Response.Write ("Please select a file to upload!"); return; }      Public BOOLisreusable {Get {            return false; }    }}

Upload pictures without refreshing and use the C # language

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.