search
Homephp教程PHP开发Detailed explanation of jquery component WebUploader file upload usage

WebUploader is a simple modern file upload component developed by the Baidu WebFE (FEX) team, mainly based on HTML5 and supplemented by FLASH. Below, I will demonstrate the usage of the jquery WebUploader file upload component.

Using WebUploader, you can also upload files in batches, support thumbnails, and many other parameter options can be set, and multiple event methods can be called. You can customize the upload components you want as you like.

Detailed explanation of jquery component WebUploader file upload usage

Next, I will use a picture upload example to explain how to use WebUploader.

HTML

We first load the css and related js files.

<link rel="stylesheet" type="text/css" href="css/webuploader.css">
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="js/webuploader.min.js"></script>

Then we need to prepare a button #imgPicker and a container #fileList to store the added file information list. Add the following code to the body:

<div id="uploadimg">
 <div id="fileList" class="uploader-list"></div>
 <div id="imgPicker">选择图片</div>
</div>

JAVASCRIPT

Create first Web Uploader example:

var uploader = WebUploader.create({
 auto: true, // 选完文件后,是否自动上传
 swf: &#39;js/Uploader.swf&#39;, // swf文件路径
 server: &#39;upload.php&#39;, // 文件接收服务端
 pick: &#39;#imgPicker&#39;, // 选择文件的按钮。可选
 // 只允许选择图片文件。
 accept: {
 title: &#39;Images&#39;,
 extensions: &#39;gif,jpg,jpeg,bmp,png&#39;,
 mimeTypes: &#39;image/*&#39;
 }
});

Then listen to the fileQueued event, that is, when a file is added, create an image preview through uploader.makeThumb.

uploader.on( &#39;fileQueued&#39;, function( file ) {
 var $list = $("#fileList"),
 $li = $(
  &#39;<div id="&#39; + file.id + &#39;" class="file-item thumbnail">&#39; +
  &#39;<img  src="/static/imghwm/default1.png"  data-src="https://img.php.cn//upload/image/676/307/619/1481092301585310.jpg?x-oss-process=image/resize,p_40"  class="lazy"  alt="Detailed explanation of jquery component WebUploader file upload usage" >&#39; +
  &#39;<div class="info">&#39; + file.name + &#39;</div>&#39; +
  &#39;</div>&#39;
  ),
 $img = $li.find(&#39;img&#39;);
  
  
 // $list为容器jQuery实例
 $list.append( $li );
  
 // 创建缩略图
 uploader.makeThumb( file, function( error, src ) {
 if ( error ) {
  $img.replaceWith(&#39;<span>不能预览</span>&#39;);
  return;
 }
  
 $img.attr( &#39;src&#39;, src );
 }, 100, 100 ); //100x100为缩略图尺寸
});

The last is the upload status prompt. When the file is uploaded, the upload is successful, the upload is failed, and the upload is completed respectively corresponds to the uploadProgress, uploadSuccess,
uploadError, and uploadComplete events.

// 文件上传过程中创建进度条实时显示。
uploader.on( &#39;uploadProgress&#39;, function( file, percentage ) {
 var $li = $( &#39;#&#39;+file.id ),
 $percent = $li.find(&#39;.progress span&#39;);
  
 // 避免重复创建
 if ( !$percent.length ) {
 $percent = $(&#39;<p class="progress"><span></span></p>&#39;)
  .appendTo( $li )
  .find(&#39;span&#39;);
 }
  
 $percent.css( &#39;width&#39;, percentage * 100 + &#39;%&#39; );
});
  
// 文件上传成功,给item添加成功class, 用样式标记上传成功。
uploader.on( &#39;uploadSuccess&#39;, function( file, res ) {
 console.log(res.filePath);//这里可以得到上传后的文件路径
 $( &#39;#&#39;+file.id ).addClass(&#39;upload-state-done&#39;);
});
  
// 文件上传失败,显示上传出错。
uploader.on( &#39;uploadError&#39;, function( file ) {
 var $li = $( &#39;#&#39;+file.id ),
 $error = $li.find(&#39;div.error&#39;);
  
 // 避免重复创建
 if ( !$error.length ) {
 $error = $(&#39;<div class="error"></div>&#39;).appendTo( $li );
 }
  
 $error.text(&#39;上传失败&#39;);
});
  
// 完成上传完了,成功或者失败,先删除进度条。
uploader.on( &#39;uploadComplete&#39;, function( file ) {
 $( &#39;#&#39;+file.id ).find(&#39;.progress&#39;).remove();
});

At this point, we have implemented a simple image upload example. Clicking "Select Image" will pop up the file selection dialog box. After selecting the image, the image upload process will be entered, and the corresponding abbreviation of the image will be displayed. The thumbnail appears in the list.

Commonly used option settings and event calls

Web Uploader provides a wealth of API option settings and event calls.

Detailed explanation of jquery component WebUploader file upload usage

Detailed explanation of jquery component WebUploader file upload usage

Commonly used event descriptions:

Detailed explanation of jquery component WebUploader file upload usage

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool