ajax 새로 고침 없 이 사진 업로드

5472 단어 Ajax
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>    </title>
    <script src="js/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        $(function() {
            $('#fileUp').change(function() {
                $('#uploadLog').html('     ....');
                $('#formFile').submit();
            });
        })
        function uploadSuccess(msg) {
            if (msg.split('|').length > 1) {
                $('#imgShow').attr('src', msg.split('|')[1]);
                $('#uploadLog').html(msg.split('|')[0]);
            } else {
                $('#uploadLog').html(msg);
            }
        }
    </script>

</head>
<body>
    <!--
               form target   ?  target    frameFile, form   iframe name ,
                  form               iframe          ,
                    !
     -->
    <form id='formFile' name='formFile' method="post" action="IbeaconHandler.ashx" target='frameFile'
    enctype="multipart/form-data">
    <input type='file' id='fileUp' name='fileUp' />
    <div id='uploadLog'>
    </div>
    <br />
    <img width='200' src='' height='200' id='imgShow' alt='   ' />
    </form>
    <!--
          iframe  post                  post       ,          ,
                ,     iframe    display:none   !                
              ,              !
    -->
    <iframe id='frameFile' name='frameFile' style='display: none;'></iframe>
</body>
</html>


    :
<%@ WebHandler Language="C#" Class="IbeaconHandler" %>

using System;
using System.Web;
using System.IO;

public class IbeaconHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        //string fname = context.Request.QueryString["op"].ToString();
        //string str =fname+"({name:'judy',age:'23'})";
         try    {
             //    Post   file    ,        <input type='file' name='fileUp'/>     
             HttpPostedFile file = context.Request.Files["fileUp"];
             if (file != null)
             {
                 //         
                 string path = context.Server.MapPath("~/images/");
                 //           
                 string imageUrl = path +Path.GetFileName(file.FileName);
                 //       
                 string ext = Path.GetExtension(file.FileName).ToLower();
                 //          
                 if (!ext.Equals(".gif") && !ext.Equals(".jpg") && !ext.Equals(".png") && !ext.Equals(".bmp"))
                 {
                     //  window.parent.uploadSuccess()           javascript function,                      
                     context.Response.Write("<script>window.parent.uploadSuccess('           !     (.gif、.jpg、.png、.bmp)');</script>");
                     context.Response.End();
                 }
                 //       
                 if (file.ContentLength > 1048576)
                 {
                     //  window.parent.uploadSuccess()           javascript function,                       
                     context.Response.Write("<script>window.parent.uploadSuccess('          1048576KB!     !');</script>");
                     context.Response.End();
                 }
                 //    
                 file.SaveAs(imageUrl);
                 //  window.parent.uploadSuccess()           javascript function,                      
                 //                   ,       |     :     |/Test/hello.jpg
                 context.Response.Write("<script>window.parent.uploadSuccess('Upload Success!|/Test/" + file.FileName + "');</script>");
                 context.Response.End();
             }
             else
             {
                 //    
                 context.Response.Write("upload lose!");
                 context.Response.End();
             }
         }
         catch
         {
             //     
             context.Response.Write("upload lose!");
             context.Response.End();
         }
        //context.Response.Write("hello word");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

좋은 웹페이지 즐겨찾기