Ajax image upload (ASP. +jquery+ashx)

Source: Internet
Author: User
Tags httpcontext

First, establish the Default.aspx page

[CSharp]View PlainCopy
  1. <%@ page language="C #" autoeventwireup="true" codefile="Default.aspx.cs" inherits="_default"% >
  2. <! DOCTYPE HTML public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">
  3. "http://www.w3.org/1999/xhtml" >
  4. "Server" >
  5. <title>ajax Photo Upload </title>
  6. <script src="js/jquery-1.3.2.min.js" type="Text/javascript" ></script>
  7. <script src="js/jquery.form.js" type="Text/javascript" ></script>
  8. <script type="Text/javascript" >
  9. function upload () {
  10. var path = document.getElementById ("File1"). Value;
  11. var img = document.getElementById ("IMG1");
  12. if ($.trim (path) = ="") {
  13. Alert ("Please select a file to upload");
  14. return;
  15. }
  16. $ ("#form1"). Ajaxsubmit ({
  17. Success:function (str) {
  18. if (str!=null && str!="undefined") {
  19. if (str = = "1") {alert ("upload succeeded");d Ocument.getelementbyid ("img1"). src="Images/logo.jpg?" +new Date (); / * Refresh picture after upload */}
  20. Else if (str=="2") {alert ("only images in JPG format can be uploaded");}
  21. Else if (str=="3") {Alert ("picture cannot be greater than 1M");}
  22. Else if (str=="4") {Alert ("Please select File to upload");}
  23. else {alert (' operation failed!  ‘);}
  24. }
  25. Else alert (' operation failed!  ‘);
  26. },
  27. Error:function (Error) {alert (error);},
  28. URL:' handler.ashx ',/ * Set the page to which post is submitted * /
  29. Type: "post",/ * Set the form to submit by post method * /
  30. DataType: "text"/ * Set return value type is text * /
  31. });
  32. }
  33. </script>
  34. <body>
  35. <form id="Form1" runat= "Server" >
  36. <input id="File1" name="File1" type="file"/>
  37. <input id="Iptup" type="button" value="upload logo" onclick="upload ()" />
  38. <img id="img1" alt="website logo" src=" images/weblogo.jpg"/>
  39. </form>
  40. </body>


Second, a new general processing file Handler.ashx

[CSharp]View PlainCopy
    1. <%@ WebHandler language="C #" class="Handler"%>
    2. Using System;
    3. Using System.Web;
    4. Public class Handler:ihttphandler {
    5. public void ProcessRequest (HttpContext context) {
    6. Httppostedfile _upfile = context.  request.files["File1"];
    7. if (_upfile = = null)
    8. {
    9. Responsewriteend (Context, "4"); Please select a file to upload
    10. }
    11. Else
    12. {
    13. string fileName = _upfile. FileName; /* Get file Name: C:\Documents and settings\administrator\ desktop \123.jpg*/
    14. string suffix = filename.substring (Filename.lastindexof (".") + 1). ToLower (); /* Get the suffix name and convert to lowercase: jpg*/
    15. int bytes = _upfile. ContentLength; //Gets the byte size of the file
    16. if (suffix! = "jpg")
    17. Responsewriteend (Context, "2"); //can only upload jpg format pictures
    18. if (bytes > 1024x768 * 1024x768)
    19. Responsewriteend (Context, "3"); //Picture cannot be greater than 1M
    20. _upfile. SaveAs (HttpContext.Current.Server.MapPath ("~/images/logo.jpg")); Save Picture
    21. Responsewriteend (Context, "1"); //Upload successful
    22. }
    23. }
    24. private void Responsewriteend (HttpContext context, string msg)
    25. {
    26. Context. Response.Write (msg);
    27. Context. Response.End ();
    28. }
    29. public bool IsReusable {
    30. get {
    31. return false;
    32. }
    33. }
    34. }

Project Structure diagram

Ajax image upload (ASP. +jquery+ashx)

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.