ajax与一般处理程序上传图片

前台html代码及juqery代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ajax上传文件.WebForm1" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ajax图片上传</title>
    <script src="js/jquery-1.4.2.min.js"></script>
    <script src="js/jquery-form.js"></script>


    <script type="text/javascript">
        $(function () {
            $('#iptUp').click(function () {
                  var path = document.getElementById("File1").value;
                var img = document.getElementById("img1");
                if ($.trim(path) == "") {
                    alert("请选择要上传的文件");
                    return;
                } else {
               $('#form1').ajaxSubmit({
                url: "Handler1.ashx", /*设置post提交到的页面*/
                type: "post", /*设置表单以post方法提交*/
                dataType: "text", /*设置返回值类型为文本*/
                success: function (str) {
                    if (str != null && str != "undefined") {
                        if (str == "1") { alert("上传成功"); document.getElementById("img1").src = "images/logo.jpg?" + new Date();/*上传后刷新图片*/ }
                        else if (str == "2") { alert("只能上传jpg格式的图片"); }
                        else if (str == "3") { alert("图片不能大于1M"); }
                        else if (str == "4") { alert("请选择要上传的文件"); }
                        else { alert('操作失败!'); }
                    }
                    else alert('操作失败!');
                },
                error: function (error) { alert("操作失败!"); }


            });




                }
            });




            


        });




    </script>
</head>
<body>
    <form id="form1" runat="server">
        <input id="File1" name="File1" type="file" />
        <input id="iptUp" type="button" value="上传Logo" />
        <img id="img1" alt="网站Logo" src="images/weblogo.jpg" />
    </form>
</body>
</html>


一般处理程序代码:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;


namespace ajax上传文件
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : 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  


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


        private void ResponseWriteEnd(HttpContext context, string msg)
        {
            context.Response.Write(msg);
            context.Response.End();
        }  


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }












    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值