ajax图片上传(jQuery+ashx)

jquery.form.js(2.64)下载:http://github.com/malsup/form/raw/master/jquery.form.js 

Default.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="js/jquery.form.js" type="text/javascript"></script>
    <script type="text/javascript">        
      function upload(){  
        var path = document.getElementById("File1").value;  
        if($.trim(path)==""){  
            alert("请选择要上传的文件");  
            return;  
            }

            $("#form1").ajaxSubmit({
                success: function(str) {
                    if (str != null && str != "undefined") {
                      if (str == "2") { alert("只能上传jpg格式的图片"); }  
                      else if(str=="3"){alert("图片不能大于1M");}  
                      else if(str=="4"){alert("请选择要上传的文件");}
                      else document.getElementById("imgName").value = str;
                    }
                    else alert('操作失败!');
                },
                error: function(error) { alert(error); },
                url: 'Handler.ashx', /*设置post提交到的页面*/
                type: "post", /*设置表单以post方法提交*/
                dataType: "text" /*设置返回值类型为文本*/
            });  
    }        
    </script>  
</head>
<body>
    <form id="form1">
        <input id="File1" name="File1" type="file" />  
        <input id="iptUp" type="button" value="上传Logo"  onclick="upload()"/> 
        <input type="text" value="" id="imgName" /> 
    </form>
</body>
</html>

Handler.ashx

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        HttpPostedFile _upfile = context.Request.Files["File1"];
        if (_upfile == null)
        {
            ResponseWriteEnd(context, "4");//请选择要上传的文件  
        }
        else
        {
            string fileName = _upfile.FileName;/*获取文件名: C:\Documents and Settings\Administrator\桌面\123.jpg*/
            string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();/*获取后缀名并转为小写: jpg*/
            int bytes = _upfile.ContentLength;//获取文件的字节大小  

            if (suffix != "jpg")
                ResponseWriteEnd(context, "2"); //只能上传JPG格式图片  
            if (bytes > 1024 * 1024)
                ResponseWriteEnd(context, "3"); //图片不能大于1M  

            //重命名:DateTime
            Random ro = new Random();
            fileName = string.Format("{0}{1}.{2}", DateTime.Now.ToString("yyyyMMddHHmmssff"), ro.Next(1000, 9999), suffix);

            _upfile.SaveAs(HttpContext.Current.Server.MapPath("~/UpLoad/" + fileName));//保存图片  
            ResponseWriteEnd(context, fileName); //上传成功  
        }  
    }

    private void ResponseWriteEnd(HttpContext context, string msg)
    {
        context.Response.Write(msg);
        context.Response.End();
    }  
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

posted @ 2014-06-15 23:32  linyongqin  阅读(261)  评论(0编辑  收藏  举报