SWFUpload 文件上传插件常用的配置讲解

html-css07

SWFUpload 文件上传插件常用的配置讲解,第1张

上传文件swfUpload 插件:

基本的文件上传涉及到的四个文件  (还有一个处理数据的php文件  这里没有涉及到)

html页面:

upload_window.html

js文件:

swfupload.js

handlers.js

fileprogress.js

第一:从html页面出发:

重要的是将页面加载时间中的var setting={}这个大对象中的参数设置好  以下都是这个对象里面的常用配置 非常重要

首先需要将swfupload.swf加载

在 var  setting={} 这个大对象里面加flash_url:值

如: flash_url: "<tpl>$siteurl_static</tpl>/assets/uc/js/swfupload.swf",

然后需要将上传的路径加入 如: upload_url: "http://load.zom.com/u.do?uploadkey=" + uploadkey + "&ck=" + ck + "&cc=" + cc,(java的上传路径)

相关的设置常用的有:

file_size_limit  (设置上传的大小)    file_types(设置文件上传的类型)file_types_description(设置文件上传描述)

file_upload_limit (设置文件上传的数量限制)file_queue_limit (设置文件队列数量限制)

prevent_swf_caching : false  (在相关的swf文件增加随机参数避免Flash被缓存)

debug:false

按钮的相关配置:

button_width: "200",

button_height: "50",

button_text_left_padding: 16,

button_text_top_padding: 7,

button_cursor:  button_cursor 指定鼠标悬停在Flash按钮上时的光标样式,可用值为SWFUpload.CURSOR里定义的常量。如:button_cursor: SWFUpload.CURSOR.HAND,

button_action(设置只能上传一个文档的限制:--》button_action: SWFUpload.BUTTON_ACTION.SELECT_FILE)

之后就是设置一些事件处理函数  这些都是在  handlers.js 里面相应的函数

file_dialog_start_handler: fileDialogStart,(设置文件对话开始函数)

file_queued_handler: fileQueued,(设置文件队列函数)

file_dialog_complete_handler: fileDialogComplete,(设置文件对话完成处理函数)

file_queue_error_handler: fileQueueError,(设置队列错误处理函数)

upload_start_handler: uploadStart,(设置开始上传函数)

upload_progress_handler: uploadProgress,(设置上传进度处理函数)

upload_error_handler: uploadError,(设置上传错误处理函数)

upload_complete_handler: uploadComplete,(设置上传完成处理函数)

upload_success_handler: uploadSuccess(设置上传成功处理函数)

以上的配置都是在页面自动加载函数的setting大对象里面需要配置的基本参数

除了以上这些还有下面相应的非常关键的配置

别忘记:在setting大对象结束之后 在自动加载函数结束之前还有swfu = new SWFUpload(settings)  实例化一个对象

var setting还有比较重要的配置  如下:

1.关于上传进度的配置是关键:

在var setting={}这个大对象里面设置一个元素:

custom_settings: {

                    progressTarget: "fsUploadProgress"

 },

progressTarget的值(即fsUploadProgress)是文件上传进度的显示 将html里面设置相应的位置放id="fsUploadProgress"

如:<div class="progressbar progressbar-0" id="fsUploadProgress">

            <span class="prog-num">0</span>

        </div>

span标签里的0就是从0开始进行上传  0就是初始的显示进度

2:关于上传的按钮设置 

在 var setting={} 这个大对象里面设置  button_placeholder_id : "spanButtonPlaceHolder"

需要将html相应的上传按钮加上相应的id="spanButtonPlaceHolder"

如:<div id="upload_doc" class="up-btn"><i >上传文档</i><span id="spanButtonPlaceHolder"></span></div>

成功上传需要将相应的数据进行处理:

在html页面中需要写ajax进行数据的处理~

如:

//成功后调用

function agree_upload(){

            var doc_id=$('.doc_title').attr('id')

            if(doc_id>0){

                uploadFinish(doc_id)

                parent.DOC88Window.close()

            }else{

                alert('您还未选择重新上传的文档')

            }

        }

        function uploadFinish(new_p_id) {

            var old_p_id = "<tpl>$p_id</tpl>"

            $.ajax({

                url: "/ucr/doc.php?act=save_upload",

                type: "post",

                data: {

                    old_p_id: old_p_id,

                    new_p_id: new_p_id

                },

                dataType: "json",

                success: function (msg) {

                    if (msg.result == 1) {

                        alert("数据正确")

                    } else {

                        alert("数据错误")

                    }

                }

            })

        }

第二:因为html页面中setting配置中有相应的函数处理配置  涉及到handler.js函数,所以接下来到handler.js文件的处理配置

根据html页面的配置 处理函数的顺序进行相应的配置

首先是fileQueue函数  文件排队函数:

需要设置一个变量 关于flash动画的函数

var stats = swfu.getStats()

根据需要将文件上传队列数量进行限制

if (stats.files_queued >1) {

            alert("您的附件不能超过1个")

                    return false

    }

接下来是fileQueueError函数  文件排队错误函数:

根据需要将相应的设置放在这个函数里面

可以放在try catch函数里面    设置的限制如下:

      switch (errorCode) {

            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:

                alert('单个文件大小不要超过50MB')

                break

            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:

                alert('不能上传空文件')

                this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message)

                break

            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:

                alert('文件类型错误')

                break

            default:

                if (file !== null) {

                }

                this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message)

                break

        }

接下来是uploadStart函数  文件上传开始函数:

设置相应的功能按钮的变换  比如上传开始(走到这个函数时 可以将相应的上传按钮改成上传中 并且禁止点击 就是禁用功能 加上一个取消上传按钮 )

可以将上传的文件的名称和文件格式显示出来 

如:

$("#upload_doc i").html("上传中")

    $('#cancel_upload').html('取消')

$("#upload_doc").attr('disabled','disabled')

var name = file.name

                $('.doc_title').html(name)

        var format = file.type

        format = format.toLocaleUpperCase()

        format = format.replace('.', '')

        $('.doc_format').html(format) 

接下来是uploadProgress函数  文件上传进度函数:如:

var percent = Math.ceil((bytesLoaded / bytesTotal) * 100)//上传的进度

        var progress = new FileProgress(file, this.customSettings.progressTarget)

        progress.setProgress(percent)

        progress.setStatus("正在上传")

接下来是uploadSuccess函数  文件上传成功函数

然后是uploadError函数  上传失败函数:

其他相关的函数可以根据需要进行设置

第三:fileprogress.js文件  关于文件上传进度  关键的是:

FileProgress函数的设置:

如:

function FileProgress(file, targetID) {

        this.fileProgressID = file.id

        this.fileProgressWrapper = document.getElementById(this.fileProgressID)

        if (!this.fileProgressWrapper) {

                  this.fileProgressWrapper = document.createElement("li")

                  this.fileProgressWrapper.id = this.fileProgressID

                  document.getElementById(targetID).appendChild(this.fileProgressWrapper)

        }

      this.setTimer(null)

}

FileProgress.prototype.setProgress = function (percentage) {}  里面进度样式的处理

如:

if (percentage <= 5) {

                $(".progressbar").addClass('progressbar-5')

    } else if (percentage <= 10) {

                $(".progressbar").addClass('progressbar-10')

    }......

第四:swfupload.js文件    几乎不用修改 可以将不用的函数删减

有很详细的讲解 链接:https://www.cnblogs.com/myboke/p/5579236.html

http://www.runoob.com/w3cnote/swfupload-guide.html

html5uploader.js 兼容ie8方法如下:

拥有两个插件,一个是flash版本的uploadify,免费的。另一个是自己写的html5版本的,名叫html5uploader(好俗的名字。。),再加一个适配器uploadadapter,用来决定在什么时候调用哪个插件。页面中只调用uploadadapter。关键的难题就在于,页面中的代码是只写一次的,不管是flash的还是html5的都得能识别出页面上的参数,这也就是我的山寨版本的插件做的事情,原flash版本的配置参数通通得识别并有效。幸好,已经实现了。

上demo

    很多东西,一上demo就都清楚了。。。

[html] view plain copy

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

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>无标题文档</title>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript" src="jquery.uploadadapter.js"></script>

<script type="text/javascript" src="jquery.loadscript.js"></script>

<style type="text/css">

</style>

<script type="text/javascript">

$(function(){

  $('#upload').uploadadapter({

      fileTypeExts:'*.jpg*.png',

      auto:false,

      showUploaded:true,

      baseurl:'.',//当前目录

      multi:true,

      removeTimeout:9999999,

      uploadurl:'upload.php'

      })

  })

</script>

</head>

<body>

<input type="file" name="file" id="upload"  />

</body>

</html>

    ie8下调用flash

   firefox、chrome下调用html5

    略有差异,ie8下圆角没了。。。如果不喜欢这个样式呢,就请打开css文件自己修改吧。

    很简单的调用,可以看到在页面中使用的是uploadadapter,由它来决定调用哪个插件。俩个插件所需要的js文件和css文件都是异步引入的,此处用到一个小插件loadScript。参数没有写全,可以自己看源代码的注释~

    uploadadapter中的调用情况是我需要的配置,你也可以随意修改。

四、相关文件注释

    在此把文件夹中的文件做一个简要介绍:

    /html5uploader   html5上传插件,你也可以拿来单独使用

    /uploadify3.2        flash上传插件,也可以拿来单独使用

    /uploads    存放上传的文件

    /jquery.loadScript.js   用于异步引入脚本的小插件

    /jquery.uploadadapter.js   适配器,用来判断客户端类型,动态调用上传插件

    /upload.php    后台处理程序,最基本的

五、上源码,注释很全哦

http://download.csdn.net/detail/never_say_goodbye/5090639

六、一个bug!!

很重的哦,我之前给疏漏了,在这里说一下,文件就不重新上传了

在jquery.html5uploader.js的158和164行,将$('.uploadify-progress')改为$('#'+file.index).fnd('.uploadify-progress'),否则上传多个文件会混淆。