代码改变世界

[ASP.NET]uploadify简单使用讲解

2014-02-14 11:05  Max蚊子  阅读(1699)  评论(0编辑  收藏  举报

背景:在用户控件中使用html 的file控件或者ASP.NET的FileUpLoad控件都无法获取到文件,于是想到听说过的uploadify

uploadify官网:www.uploadify.com

直接官网下载相关文件,发现里面有几个php文件,为了不用还配置php于是修改为ASP.NET用法

添加UploadHandler.ashx文件及修改代码:

public class UploadHandler : 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("1");
            }
            else
            {
                context.Response.Write("0");
            }
        }

 

在ascx页面引入3.1版本的uploadify

<link rel="stylesheet" type="text/css" href="../jslib/uploadify/uploadify.css" />
<script type="text/javascript" src="../jslib/uploadify/jquery-1.7.2.min.js"></script>//uploadify官网说明1.7.2+,既然推荐1.7.2那就是这个版本
<script type="text/javascript" src="../jslib/uploadify/jquery.uploadify.min.js"></script>

在加入js

 

$(function() {
    $('#ftcFormHolder_FpgForm_btnHtmlFile1').uploadify({
        'swf'      : '../jslib/uploadify/uploadify.swf',
        'uploader' : '../jslib/uploadify/UploadHandler.ashx', 
        'buttonText': '浏览',
        'multi': false,
        'onUploadComplete' : function(file) {
            alert('The file ' + file.name + ' finished processing.');
        } 
    });
});

 

 

以上为简单使用,其他参数或者事件可参考官网

 

 

 

网站:feiger.cn         飞鸽博客,关注互联网、站长圈的程序员博客!