jquery上传文件

http://hi.baidu.com/shouxin1014/blog/item/43aa70504fb6f6998c5430b5.html

客户端代码:

<link href="uploadJs/jquery.uploadify-v2.1.4/uploadify.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" src="uploadJs/jquery.uploadify-v2.1.4/jquery-1.4.2.min.js"></script>

    <script type="text/javascript" src="uploadJs/jquery.uploadify-v2.1.4/swfobject.js"></script>

    <script type="text/javascript" src="uploadJs/jquery.uploadify-v2.1.4/jquery.uploadify.v2.1.4.min.js"></script>

    <script type="text/javascript">    
        $(document).ready(function()
        {
            $("#uploadify").uploadify({
                'uploader': '/uploadJs/jquery.uploadify-v2.1.4/uploadify.swf',
                'script': '/uploadJs/uploadfile.ashx',//后台处理
                'cancelImg': '/uploadJs/jquery.uploadify-v2.1.4/cancel.png',//单个取消按钮
                'folder': '/uploadfile',//文件夹
                'queueID': 'fileQueue',//文件队列
                'auto': false,
                'multi': true,//多文件上传,
                'simUploadLimit':'3',//允许多文件上传的文件数
                'fileExt':'*.doc;*.pdf;*.rar;*.zip;*.7z;*.chm',//.jpg,.bmp,.gif,.png,.doc,.pdf,.rar,.zip,.7z,
                'fileDesc':'*.doc;*.pdf;*.rar;*.zip;*.7z;*.chm',
                'buttonText':'select',
                'buttonImg':'/images/gerennav2.gif',
                'width':'100',
                'height':'32',
                'sizeLimit':'6000000',//大小限制,6M                
                onError:function(event, queueID, fileObj)
                {
                     alert('error '+d.type+": "+d.info);                  
                },
                onComplete:function(event, queueID, fileObj, response, data)
                {
                    //alert("成功上传,平均速率:"+data.speed+"kb/s");
                    if(response=="0")
                    $("#haveupfile").append("<p style='color:#f00'>上传"+fileObj.name+"失败!<a onclick='deletefile(\"\",this)'>删除</a></p>");
                    else if(response=="1")
                    $("#haveupfile").append("<p style='color:#f00'>"+fileObj.name+"文件格式错误!<a onclick='deletefile(\"\",this)'>删除</a></p>");
                    else
                    $("#haveupfile").append("<p>上传<b>"+response+"</b>成功!<a onclick='deletefile(\""+response+"\",this)' style='cursor:pointer;'>删除</a></p>");
                },
//                onAllComplete:function(e,d)
//                {
//                    //alert("成功上传"+d.filesUploaded+"个文件,错误:"+d.errors);
//                },
                onSelect:function(e,q,f)
                {
                    if(f.size>5242880)
                    {
                        alert("文件大小超过限制!请重新选择");
                        uploadifyCancel(q);                        
                    }                                    
                }
            });
        }); 
        function deletefile(url,obj){
            if(url!="")
            {
                $.post("/uploadJs/delfile.ashx",{"fileurl":url},function(_result){
                    if(_result=="1")alert("删除成功!");
                    else if(_result=="2")alert("删除异常!");
                    else if(_result=="3")alert("请求失败!");
                    else if(_result=="4")alert("不存在该文件!");
                    $(obj).parent().remove();
                });
            }
            else
            $(obj).parent().remove();
        } 
    </script>

html:

<div id="fileQueue">
    </div>
    <input type="file" name="uploadify" id="uploadify" />
    <p>
        <a href="javascript:$('#uploadify').uploadifyUpload()">上传</a> | <a href="javascript:$('#uploadify').uploadifyClearQueue()">
            取消上传</a>
    </p>
    <div id="haveupfile"></div>

上传后台:

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

using System;
using System.Web;
using System.IO;
public class uploadfile : IHttpHandler
{
    public string extensionlimit = ".jpg,.bmp,.gif,.png,.doc,.pdf,.rar,.zip,.7z,";//用逗号隔开,小写..
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Charset = "utf-8";

        HttpPostedFile file = context.Request.Files["Filedata"];
        string thepath = @context.Request["folder"];
        string uploadPath = HttpContext.Current.Server.MapPath(thepath);

        if (file != null)
        {
            if (extensionlimit.IndexOf(System.IO.Path.GetExtension(file.FileName).ToLower() + ",") > -1)
            {
                thepath += "\\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString();
                uploadPath += "\\" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString();
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                string reFile = "OA" + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + "-";//前缀文件名
                file.SaveAs(uploadPath + "\\" + reFile + file.FileName);
                //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
                //返回网络路径
                thepath += "\\" + reFile + file.FileName;
                thepath = thepath.Replace("\\", "/");
                context.Response.Write(thepath);
            }
            else
            {
                context.Response.Write("1");//扩展验证失败
            }
        }
        else
        {
            context.Response.Write("0");//验证失败
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

删除后台:

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

using System;
using System.Web;
using System.IO;
public class delfile : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Charset = "utf-8";
        if (context.Request.Form["fileurl"] != null)
        {
            string thefile = HttpContext.Current.Server.MapPath(context.Request.Form["fileurl"]);
            if (File.Exists(thefile))
            {
                try
                {
                    File.Delete(thefile);
                    context.Response.Write("1");
                }
                catch { context.Response.Write("2"); }
            }
            else
            {
                context.Response.Write("4");
            }
        }
        else
        {
            context.Response.Write("3");
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

项目路径

你可能感兴趣的:(jquery)