Home > Web Front-end > JS Tutorial > body text

Analysis of jQuery file upload control Uploadify

不言
Release: 2018-06-25 11:46:10
Original
1708 people have browsed it

This article mainly introduces the usage of jQuery file upload control Uploadify. The original author made very detailed remarks. Friends in need can refer to it.

File upload control based on jquery, supports ajax upload without refresh, uploads multiple files at the same time, displays the upload progress, and deletes uploaded files.

Requires the use of jquery1.4 or above, flash player 9.0.24 or above.

There are two versions, one using flash and one using html5. HTML5 requires payment ~ so here we only talk about the usage of the flash version.

Official website: http://www.uploadify.com/

Control screenshot:

Analysis of jQuery file upload control Uploadify

Usage:

First reference the following file

<link rel="stylesheet" type="text/css" href="uploadify.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="jquery.uploadify-3.1.min.js"></script>
Copy after login

Create a file input, or any other element with an ID, and initialize Uploadify for it (note that there must be the file uploadify.swf in the directory, and uploadify .php background file, the path is according to the directory structure in the project)

<input type="file" name="file_upload" id="file_upload" />
<script>
 $(function(){
    $(&#39;#file_upload&#39;).uploadify({
     &#39;swf&#39;  :&#39;uploadify.swf&#39;,
      &#39;uploader&#39;:&#39;uploadify.php&#39;
     // Put your options here
    });
   });
</script>
Copy after login

In this way, a most basic upload structure is completed. The basic principle is to change the file input you created to generate a DOM structure, create a p button, modify the button style in .uploadify-button in the uploadify.css file, and position the swf file above the button, so that when you click the button, it actually The click is swf

Option:

$(&#39;#file_upload&#39;).uploadify({
 auto:false, 
 //接受true or false两个值,当为true时选择文件后会自动上传;为false时只会把选择的文件增加进队列但不会上传,这时只能使用upload的方法触发上传。不设置auto时默认为true
 buttonClass: "some-class", 
 //设置上传按钮的class
 buttonCursor: &#39;hand&#39;,
 //设置鼠标移到按钮上的开状,接受两个值&#39;hand&#39;和&#39;arrow&#39;(手形和箭头)
 buttonImage: &#39;img/browse-btn.png&#39;, 
 //设置图片按钮的路径(当你的按钮是一张图片时)。如果使用默认的样式,你还可以创建一个鼠标悬停状态,但要把两种状态的图片放在一起,并且默认的放上面,悬停状态的放在下面(原文好难表达啊:you can create a hover state for the button by stacking the off state above the hover state in the image)。这只是一个比较便利的选项,最好的方法还是把图片写在CSS里面。
 buttonText: &#39;<p>选择文件</p>&#39;,
 //设置按钮文字。值会被当作html渲染,所以也可以包含html标签
 checkExisting: &#39;/uploadify/check-exists.php&#39;,
 //接受一个文件路径。此文件检查正要上传的文件名是否已经存在目标目录中。存在时返回1,不存在时返回0(应该主要是作为后台的判断吧),默认为false
 debug: false,
 //开启或关闭debug模式
 fileObjName:&#39;filedata&#39;,
 //设置在后台脚本使用的文件名。举个例子,在php中,如果这个选项设置为&#39;the_files&#39;,你可以使用$_FILES[&#39;the_files&#39;]存取这个已经上传的文件。
 fileSizeLimit:&#39;100MB&#39;,
 //设置上传文件的容量最大值。这个值可以是一个数字或者字符串。如果是字符串,接受一个单位(B,KB,MB,GB)。如果是数字则默认单位为KB。设置为0时表示不限制
 fileTypeExts: &#39;*.*&#39;,
 //设置允许上传的文件扩展名(也就是文件类型)。但手动键入文件名可以绕过这种级别的安全检查,所以你应该始终在服务端中检查文件类型。输入多个扩展名时用分号隔开(&#39;*.jpg;*.png;*.gif&#39;)
 fileTypeDesc: &#39;All Files&#39;,
 //可选文件的描述。这个值出现在文件浏览窗口中的文件类型下拉选项中。(chrome下不支持,会显示为&#39;自定义文件&#39;,ie and firefox下可显示描述)
 formData: {
  timestamp: &#39;<?php echo $timestamp;?>&#39;,
  token: &#39;<?php echo md5(&#39;unique_salt&#39; . $timestamp);?>&#39;
 },
 //通过get或post上传文件时,此对象提供额外的数据。如果想动态设置这些值,必须在onUploadStartg事件中使用settings的方法设置。在后台脚本中使用 $_GET or $_POST arrays (PHP)存取这些值。看官网参考写法:http://www.uploadify.com/documentation/uploadify/formdata/
 height: 30,
 //设置按钮的高度(单位px),默认为30.(不要在值里写上单位,并且要求一个整数,width也一样)
 width: 120,
 //设置按钮宽度(单位px),默认120
 itemTemplate:false,
 //模板对象。给增加到上传队列中的每一项指定特殊的html模板。模板格式请看官网http://www.uploadify.com/documentation/uploadify/itemtemplate/
 method:&#39;post&#39;,
 //提交上传文件的方法,接受post或get两个值,默认为post
 multi: false,
 //设置是否允许一次选择多个文件,true为允许,false不允许
 overrideEvents: [],
 //重写事件,接受事件名称的数组作为参数。所设置的事件将可以被用户重写覆盖
 preventCaching:true,
 //是否缓存swf文件。默认为true,会给swf的url地址设置一个随机数,这样它就不会被缓存。(有些浏览器缓存了swf文件就会触发不了里面的事件--by rainweb)
 progressData: &#39;percentage&#39;,
 //设置文件上传时显示数据,有‘percentage&#39; or ‘speed&#39;两个参数(百分比和速度)
 queueID: false,
 //设置上传队列DOM元素的ID,上传的项目会增加进这个ID的DOM中。设置为false时则会自动生成队列DOM和ID。默认为false
 queueSizeLimit: 999,
 //设置每一次上传队列中的文件数量。注意并不是限制总的上传文件数量(那是uploadLimit).如果增加进队列中的文件数量超出这个值,将会触发onSelectError事件。默认值为999
 removeCompleted: true,
 //是否移除掉队列中已经完成上传的文件。false为不移除
 removeTimeout: 3,
 //设置上传完成后删除掉文件的延迟时间,默认为3秒。如果removeCompleted为false的话,就没意义了
 requeueErrors: false,
 //设置上传过程中因为出错导致上传失败的文件是否重新加入队列中上传
 successTimeout: 30,
 //设置文件上传后等待服务器响应的秒数,超出这个时间,将会被认为上传成功,默认为30秒
 swf: &#39;uploadify.swf&#39;,
 //swf的相对路径,必写项
 uploader: &#39;uploadify.php&#39;
 //服务器端脚本文件路径,必写项
 uploadLimit: 999
 //上传文件的数量。达到或超出这数量会触发onUploadError方法。默认999
})
Copy after login

Event:

$(&#39;#file_upload&#39;).uploadify({ 
 onCancel: function(file){
   console.log(&#39;The file&#39;+ file.name + &#39;was cancelled.&#39;)
 },
 //文件被移除出队列时触发,返回file参数
 onClearQueue: function(queueItemCount){
  console.log(queueItemCount+&#39;file(s) were removed frome the queue&#39;)
 },
 //当调用cancel方法且传入&#39;*&#39;这个参数的时候触发,其实就是移除掉整个队列里的文件时触发,上面有说cancel方法带*时取消整个上传队列
 onDestroy: function(){
  //调用destroy方法的时候触发
 },
 onDialogClose: function(queueData){
  console.log(queueData.filesSelected+&#39;\n&#39;+queueData.filesQueued+&#39;\r\n&#39;+queueData.filesReplaced+&#39;\r\n&#39;+queueData.filesCancelled+&#39;\r\n&#39;+ queueData.filesErrored)
 },
 //关闭掉浏览文件对话框时触发。返回queueDate参数,有以下属性:
 /*
  filesSelected 浏览文件对话框中选取的文件数量
  filesQueued 加入上传队列的文件数
  filesReplaced 被替换的文件个数
  filesCancelled 取消掉即将加入队列中的文件个数
  filesErrored 返回错误的文件个数
 */
 onDialogOpen:function(){
  //打开选择文件对话框时触发
 },
 onDisable:function(){
  //禁用uploadify时触发(通过disable方法)
 },
 onEnalbe: function(){
  //启用uploadify时触发(通过disable方法)
 },
 onFallback:function(){
  //在初始化时检测不到浏览器有兼容性的flash版本时实触发
 },
 onInit: function(instance){
  console.log(&#39;The queue ID is&#39;+ instance.settings.queueID)
 },
 //每次初始化一个队列时触发,返回uploadify对象的实例
 onQueueComplete:function(queueData){
  console.log(queueData.uploadsSuccessful+&#39;\n&#39;+queueData.uploadsErrored)
 },
 //队列中的文件都上传完后触发,返回queueDate参数,有以下属性:
 /*
  uploadsSuccessful 成功上传的文件数量
  uploadsErrored 出现错误的文件数量
 */
 onSelect: function(file){
  console.log(file.name)
 },
 //选择每个文件增加进队列时触发,返回file参数
 onSelectError: function(file,errorCode,errorMsg){
  console.log(errorCode)
  console.log(this.queueData.errorMsg)
 },
 //选择文件出错时触发,返回file,erroCode,errorMsg三个参数
 /*
  errorCode是一个包含了错误码的js对象,用来查看事件中发送的错误码,以确定错误的具体类型,可能会有以下的常量:
  QUEUE_LIMIT_EXCEEDED:-100 选择的文件数量超过设定的最大值;
  FILE_EXCEEDS_SIZE_LIMIT:-110 文件的大小超出设定
  INVALID_FILETYPE:-130 选择的文件类型跟设置的不匹配

  errorMsg 完整的错误信息,如果你不重写默认的事件处理器,可以使用‘this.queueData.errorMsg&#39; 存取完整的错误信息
 */
 onSWFReady: function(){
  //swf动画加载完后触发,没有参数
 },
 onUploadComplete: function(file){
  //在每一个文件上传成功或失败之后触发,返回上传的文件对象或返回一个错误,如果你想知道上传是否成功,最后使用onUploadSuccess或onUploadError事件
 },
 onUploadError: function(file,errorCode,erorMsg,errorString){
 },
 //一个文件完成上传但返回错误时触发,有以下参数
 /*
  file 完成上传的文件对象
  errorCode 返回的错误代码
  erorMsg 返回的错误信息
  errorString 包含所有错误细节的可读信息
 */
 onUploadProgress: function(file,bytesUploaded,bytesTotal,totalBytesUploaded,totalBytesTotal){
  $(&#39;#pregress&#39;).html(&#39;总共需要上传&#39;+bytesTotal+&#39;字节,&#39;+&#39;已上传&#39;+totalBytesTotal+&#39;字节&#39;)
 },
 //每更新一个文件上传进度的时候触发,返回以下参数
 /*
  file 正上传文件对象
  bytesUploaded 文件已经上传的字节数
  bytesTotal 文件的总字节数
  totalBytesUploaded 在当前上传的操作中(所有文件)已上传的总字节数
  totalBytesTotal 所有文件的总上传字节数
 */
 onUploadStart: function(file){
  console.log(&#39;start update&#39;)
 },
 //每个文件即将上传前触发
 onUploadSuccess: function(file,data,respone){
  alert( &#39;id: &#39; + file.id
    + &#39; - 索引: &#39; + file.index
          + &#39; - 文件名: &#39; + file.name
          + &#39; - 文件大小: &#39; + file.size
          + &#39; - 类型: &#39; + file.type
          + &#39; - 创建日期: &#39; + file.creationdate
          + &#39; - 修改日期: &#39; + file.modificationdate
          + &#39; - 文件状态: &#39; + file.filestatus
          + &#39; - 服务器端消息: &#39; + data
          + &#39; - 是否上传成功: &#39; + response);
 }
 //每个文件上传成功后触发    
})
Copy after login

Method:

Uploadify uses the plug-in mode recommended by jquery, which means that all method calls are kept in a namespace. To call these different methods, you only need to pass the method as the first parameter into uploadify and call it. Adding parameters after these methods will be passed into this method (the translation of these two sentences is not very smooth, the original text is attached: To use the different method calls, simply call Uploadify on the DOM element with the method call as the first argument.Any additional arguments added after the method name are passed to the method.)

cancel: Cancel the first uploaded file. If it is followed by the parameter "*", it will cancel the entire upload queue, such as $(el) .uploadify('cancel', '*')

upload: Upload the first uploaded file. If the parameter "*" is followed, the entire queue will be uploaded, the same as cancel

destroy: Remove the upload component and upload according to the default method of html

disable: There are two parameters, true or false, indicating whether to prohibit the upload button, true means prohibited, false means allowing uploading

settings: Return or update the method value of an instance. When a parameter with a method name is accepted, the value of that method is returned. When followed by a parameter, the value of that method is updated. Such as

$(el).uploadify(&#39;settings&#39;,&#39;buttonText&#39;,&#39;BROWSE&#39;); //设置buttonText的值为BROWSE
$(el).uploadify(&#39;settings&#39;,&#39;buttonText&#39;) //返回buttonText的值
Copy after login

stop: Stop the file or queue being uploaded

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the analysis of AngularJs Forms

The problem of image loading failure in jQuery

The above is the detailed content of Analysis of jQuery file upload control Uploadify. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template