jquery文件上传控件 Uploadify 问题记录

Uploadify v3.2.1

 

 

首先引用下面的文件

<!--上传控件 uploadify-->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<link href="~/uploadify/uploadify.css" rel="stylesheet" />
<script src="~/uploadify/jquery.uploadify.js"></script>

 

 

 

                              <div class="col-md-10">
                                <p>
                                    <!--记录地址-->
                                    <input type="text" name="UR_Icon" id="UR_Icon" class="form-control" value="@Model.xxxx" />
                                </p>
                                <!--显示缩略图-->
                                <img id="showImage" />
                                <!--uploadify插件-->
                                <input type="file" name="file_upload" id="uploadify" />
                                <p>
                                    <button type="button" class="btn btn-default" id="upload">上传</button>
                                    <button type="button" class="btn btn-default" id="cancel">取消上传</button>
                                </p>
                            </div>

 

 

 

<script type="text/javascript">
    $('#uploadify').uploadify({
        uploader: '/FileUpLoad/UpLoadProcess',           // 服务器端处理地址
        swf: '/uploadify/uploadify.swf',    // 上传使用的 Flash

        width: 90,                          // 按钮的宽度
        height: 33,                         // 按钮的高度
        buttonText: "选择图片",                 // 按钮上的文字
        buttonCursor: 'hand',                // 按钮的鼠标图标

        fileObjName: 'Filedata',            // 上传参数名称

        // 两个配套使用
        fileTypeExts: "*.jpg;*.png;*.gif",             // 扩展名
        fileTypeDesc: "请选择 jpg png gif 文件",     // 文件说明

        auto: false,                // 选择之后,自动开始上传
        multi: false,               // 是否支持同时上传多个文件
        queueSizeLimit: 1,          // 允许多文件上传的时候,同时上传文件的个数

        'itemTemplate': '<div id="${fileID}" class="uploadify-queue-item">\
                    <div class="cancel">\
                        <a href="javascript:$(\'#${instanceID}\').uploadify(\'cancel\', \'${fileID}\')">X</a>\
                    </div>\
                    <span class="fileName">${fileName} (${fileSize})</span><span class="data"></span>\<div class="uploadify-progress"><div class="uploadify-progress-bar"><!--Progress Bar--></div></div>\
                </div>',
        //从服务器端脚本返回数据
        'onUploadSuccess': function (file, data, response) {
            //设置缩略图的宽高
            $('#showImage').attr('src', data).attr('style', 'width:90px;height:90px');
            //给【input】 UR_Icon 控件赋值
            $('#UR_Icon').val(data);
            //alert('id: ' + file.id
            //            + ' - 索引: ' + file.index
            //            + ' - 文件名: ' + file.name
            //            + ' - 文件大小: ' + file.size
            //            + ' - 类型: ' + file.type
            //            + ' - 创建日期: ' + file.creationdate
            //            + ' - 修改日期: ' + file.modificationdate
            //            + ' - 文件状态: ' + file.filestatus
            //            + ' - 服务器端消息: ' + data
            //            + ' - 是否上传成功: ' + response);

        }
    });

    //上传开始
    $('#upload').on('click', function () {
        $('#uploadify').uploadify('upload', '*')
    })
    //取消上传
    $('#cancel').on('click', function () {
        $('#uploadify').uploadify('cancel', '*')
    })

</script>

 

 

服务器

public ActionResult UpLoadProcess(HttpPostedFileBase Filedata)
        {
            string now = DateTime.Now.ToString("yyyy-MM-dd");
            string upLoad = "UpLoad";

            // 如果没有上传文件
            if (Filedata == null ||
                string.IsNullOrEmpty(Filedata.FileName) ||
                Filedata.ContentLength == 0)
            {
                return this.HttpNotFound();
            }

            //获取文件的保存路径
            string uploadPath = Server.MapPath("\\" + upLoad + "\\" + now);
            //判断上传的文件是否为空
            if (Filedata != null)
            {
                //如果没有UpLoad这个文件夹
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }


            }

            // 保存到 ~/UpLoad 文件夹中,名称不变
            string filename = Guid.NewGuid().ToString("N") + Path.GetExtension(Filedata.FileName);
            string virtualPath =
                string.Format("/" + upLoad + "/{0}/{1}", now, filename);
            // 文件系统不能使用虚拟路径
            string path = this.Server.MapPath(virtualPath);

            Filedata.SaveAs(path);

            //返回虚拟路径
            return Content(virtualPath);
        }

 

 

不同地方,视图要修改的参数。

 

 

 

 

问题集锦:

1.先点击【修改窗口】,选择文件,但是没有传就关闭 modal 了。再在【添加】按钮里面,这个进度条还在?

 

 

顺带把img的地址清楚掉。

 

2.添加按钮和修改按钮的逻辑问题,自己看注释

//添加
    $('#mytool').on('click', 'button#addModel', function () {

        //加载页面基本信息
        $.ajax({
            url: "/AdminUser/AdminUserForm",
            type: "post",
            //参数:(html5:MenuForm页面html数据)
            success: function (html5) {
                //只有在没赋值的情况下,才创建
                if ($("#createModal").html() == "") {

                    $("#createModal").html(html5);
                    
                    //弹出框show
                    $("#myModal").modal("show");
                } else {
                    //点“添加”,清除掉进度条
                    $('#uploadify-queue').html('');

                    //重置添加 modal 里面的 input 的值为 null
                    $("#formMenu input[type='text']").val('');
                    //让select 选择 +<option selected="selected" value="-1">请选择一项数据!</option>
                    $("#formMenu select").val('-1'); 
                }
                //重置添加 modal 里面的 img 的值为 默认图片
                $('#showImage').attr('src', 'UpLoad/image.png').attr('style', 'width:200px;height:150px');
            }
        });
    })



    /**
    *  编辑
    */
    $('#dataTables-example tbody').on('click', 'button#editrow', function () {
        //当前行数据
        var rows = tables.row($(this).parents('tr')).data();
        //加载页面基本信息
        $.ajax({
            url: "/AdminUser/AdminUserForm",
            type: "post",
            data: rows,
            success: function (data) {
                var exp = rows.UR_Icon;

                //只有在没赋值的情况下,才创建
                $("#createModal").html(data);
                //缩略图赋值
                $('#showImage').attr('src', $('#UR_Icon').val()).attr('style', 'width:200px;height:150px');
                //弹出框show
                $("#myModal").modal("show");


                //必须先赋值,才能修改缩略图
                if (exp == null || typeof (exp) == "undefined" || exp == 0) {
                    //重置添加 modal 里面的 img 的值为 null(去掉图片缩略图)
                    $('#showImage').attr('src', 'UpLoad/image.png');
                }
            }
        });

    });

 

 

3.uploadify “ID SWFUpload_0 is already in use. The Flash Object could not be added”错误的解决方法

 

在使用 uploadify时 遇到同时加载的多个页面中包含uploadify组件时就会出现“ID SWFUpload_0 is already in use. The Flash Object could not be added”的错误,分析代码就会发现,时因为名字累加的问题,解决方法如下,

找到this.movieName,第72行 (jquery.uploadify.js)
 
this.movieName = "SWFUpload_" + SWFUpload.movieCount++;   //不能有效累加导致出现重名现象 
 
修改为随机   this.movieName = "SWFUpload_" + 

parseInt(100*Math.random());

注意:同时,<input type="file" name="file_upload" id="uploadify001" />

image

命名不能相同,这个要注意

 

 

4.“编辑”按钮,加载不出图片

 

image

 

image

 

解决方案:

image

 

参考:

http://www.cnblogs.com/haogj/archive/2013/04/27/3046138.html

 

http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html

 

http://www.buyuer.com/javaScript/61.html   SWFUpload_0 is already in use. The Flash Object could not be added

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jquery Uploadify 是一个基于 Jquery文件插件,它使用 Flash 技术来实现文件功能。它简单易用,可以方便地进行文件,并提供了多个自定义选项,如文件大小限制、文件类型限制等。 H5 上指的是使用 HTML5 中的 File API 来实现文件功能。与统的 Flash、Java Applet 等方式相比,H5 上更加安全、快速,并且不需要额外安装插件。 使用 Jquery Uploadify 进行 H5 上,可以通过以下步骤进行操作: 1. 下载并引入 Jquery Uploadify 插件文件,并确保引入了 jQuery 库。 2. 在 HTML 文件中创建一个显示上文件区域的容器,如: <div id="file-upload-container"> <input type="file" id="file-upload" name="file-upload" multiple> </div> 3. 在 JavaScript 文件中,使用 Jquery 的选择器选取文件区域,然后调用 Uploadify 插件的方法进行初始化,如: $("#file-upload").uploadify({ swf: 'uploadify.swf', // 指定 Flash 文件路径 uploader: 'uploadify.php', // 指定上文件的服务器端处理文件路径 // 其他自定义选项... }); 4. 在服务器端编写处理文件的脚本(uploadify.php)。可以使用 PHP、Java、Python 等服务器端语言进行处理,并根据上结果返回相应的提示信息。 通过以上步骤,我们就可以使用 Jquery Uploadify 插件进行 H5 文件了。需要注意的是,由于 H5 上只能在现代浏览器中完全支持,所以在使用时应该检查浏览器兼容性,确保用户能够正常上文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值