C#.net中jquery实现文件上传

页面代码:

NOTE:需要jquery插件(Uploadify)下载地址:uploadify下载

<%@ Page Language="C#" CodeBehind="AsynchronousUpload.aspx.cs"
    Inherits="Example.AsynchronousUpload" %>

<!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 runat="server">
    <title></title>
    <script src="Scripts/uploadify-v2.1.4/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script src="Scripts/uploadify-v2.1.4/jquery.uploadify.v2.1.4.js" type="text/javascript"></script>
    <script src="Scripts/uploadify-v2.1.4/jquery.uploadify.v2.1.4.min.js" type="text/javascript"></script>
    <script src="Scripts/uploadify-v2.1.4/swfobject.js" type="text/javascript"></script>
    <link href="Scripts/uploadify-v2.1.4/uploadify.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        $(document).ready(function () {
            $("#uploadify").uploadify({
                'uploader': 'Scripts/uploadify-v2.1.4/uploadify.allglyphs.swf',
                'script': 'handler/upHandle.ashx',
                'cancelImg': 'Scripts/uploadify-v2.1.4/cancel.png',
                'folder': 'UpFile', //上传文件路径
                'queueID': 'fileQueue',
                'auto': false, //是否需要点击上传才上传文件
                'multi': false, //是否允许上传多个文件
                'fileDesc': '请选择图片类型',
                'fileExt': '*.jpg',
                'sizeLimit': '1048576', //限制大小1M
                onComplete: function (evt, queueID, fileObj, response, data) {
                    alert(response);
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <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>
    </form>
</body>
</html>
一般处理程序代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace Example.handler
{
    /// <summary>
    /// upHandle 的摘要说明
    /// </summary>
    public class upHandle : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";

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

            if (file != null)
            {
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                file.SaveAs(uploadPath + file.FileName);
                //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
                context.Response.Write("上传成功");
            }
            else
            {
                context.Response.Write("0");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

posted @ 2013-05-19 12:38  BicycleBoy  阅读(1136)  评论(0编辑  收藏  举报