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

背景:在用户控件中使用html 的file控件或者ASP.NET的FileUpLoad控件都没法获取到文件,因而想到据说过的uploadifyjavascript

uploadify官网:www.uploadify.comphp

直接官网下载相关文件,发现里面有几个php文件,为了避免用还配置php因而修改成ASP.NET用法css

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

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版本的uploadifyjava

<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>

在加入jsjquery

 

$(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.');
        } 
    });
});

 

 

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

相关文章
相关标签/搜索