惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
小众软件
小众软件
量子位
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Jina AI
Jina AI
T
Threat Research - Cisco Blogs
博客园_首页
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Security Latest
Security Latest
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
Lohrmann on Cybersecurity
V
V2EX
P
Proofpoint News Feed
I
Intezer
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
罗磊的独立博客
H
Help Net Security
T
Tor Project blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Schneier on Security
Blog — PlanetScale
Blog — PlanetScale
L
LINUX DO - 热门话题
D
DataBreaches.Net
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
P
Proofpoint News Feed
NISL@THU
NISL@THU
Y
Y Combinator Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
P
Palo Alto Networks Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Security @ Cisco Blogs

博客园 - tubo

依赖Asp.net Core的类库 找一款可以插入数学公式的思维导图软件 联想个人云开启Docker并安装常用的Docker 联想个人云无损开启SSH 我只想在Windows Server 2019上build 基于Linux的Docker Gerrit 添加用户 Windows 10 安装过程中,在自定义登录页面进入审核模式 mac远程桌面连接windows 8.1 update,提示: 远程桌面连接无法验证您希望连接的计算机的身份 iOS开发点滴:iPhone屏幕适配 C# Winform中如何让PictureBox的背景透明 ExtJS扩展:扩展grid之toolbar button禁用表达式 ExtJS扩展:扩展grid 用NuGet管理程序包时,如果解决方案目录下的packages文件夹中某一个程序包存在2个以上的版本,则会提示错误 User Profile Service服务未登录,无法加载用户配置文件 可动态构造查询条件的表达式类库(1) Windows 8安装杀毒软件avast后,不能用Sysprep进行通用化封装 Nuget 启用数据库迁移的时候一定要把包含DbContext的项目设为启动项目 “PostSharp21”任务意外失败 为什么TFS会自动签出解决方案文件(.sln)?
Extjs扩展:封装Plupload
tubo · 2013-11-07 · via 博客园 - tubo

关于Plupload

Plupload是一个web文件上传组件,支持通过HTML5、Silverlight、Flash或者普通的form来上传文件,提供了过滤文件类型、设置上传文件大小、上传进度、针对图片的缩放上传的特性,在使用上也非常简单:

   1:  var uploader = new plupload.Uploader({
   2:          runtimes : 'html5,flash,silverlight,html4',
   3:          browse_button : 'pickfiles', // you can pass in id...
   4:          container: document.getElementById('container'), // ... or DOM Element itself
   5:          url : 'upload.php',
   6:          flash_swf_url : '../js/Moxie.swf',
   7:          silverlight_xap_url : '../js/Moxie.xap',
   8:          
   9:          filters : {
  10:                  max_file_size : '10mb',
  11:                  mime_types: [
  12:                          {title : "Image files", extensions : "jpg,gif,png"},
  13:                          {title : "Zip files", extensions : "zip"}
  14:                  ]
  15:          },
  16:   
  17:          init: {
  18:                  PostInit: function() {
  19:                          document.getElementById('filelist').innerHTML = '';
  20:   
  21:                          document.getElementById('uploadfiles').onclick = function() {
  22:                                  uploader.start();
  23:                                  return false;
  24:                          };
  25:                  },
  26:   
  27:                  FilesAdded: function(up, files) {
  28:                          plupload.each(files, function(file) {
  29:                                  document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
  30:                          });
  31:                  },
  32:   
  33:                  UploadProgress: function(up, file) {
  34:                          document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
  35:                  },
  36:   
  37:                  Error: function(up, err) {
  38:                          document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
  39:                  }
  40:          }
  41:  });
  42:   
  43:  uploader.init();

Plupload的开源协议采用的是GPLv2,同时也提供商业授权

注意:目前Plupload的版本是2.0,而官方网站的文档是老版本的,最新的文档去Plupload的Github上看

封装Plupload

在Extjs中定义一个Plupload,方便其他地方使用:

   1:  Ext.define('Ext.ux.Plupload', {
   2:      constructor: function (config) {
   3:          var me = this;
   4:          var cfg = config || {};
   5:          if (!cfg.pluploadConfig) {
   6:              cfg.pluploadConfig = {};
   7:          }
   8:          else {
   9:              var tmp_cfg = cfg.pluploadConfig;
  10:              cfg.pluploadConfig = {};
  11:              Ext.Object.merge(cfg.pluploadConfig, tmp_cfg);
  12:          }
  13:          if (!cfg.pluploadConfig.runtimes) {
  14:              var runtimes = 'html5';
  15:              if (cfg.pluploadConfig.flash_swf_url) {
  16:                  runtimes += ',flash';
  17:              }
  18:              if (cfg.pluploadConfig.silverlight_xap_url) {
  19:                  runtimes += ',silverlight';
  20:              }
  21:              runtimes += ',html4';
  22:              cfg.pluploadConfig.runtimes = runtimes;
  23:          }
  24:          if (!cfg.pluploadConfig.filters) {
  25:              cfg.pluploadConfig.filters = {
  26:                  max_file_size: '10mb'
  27:              }
  28:          }
  29:          else {
  30:              if (!cfg.pluploadConfig.filters.max_file_size) {
  31:                  cfg.pluploadConfig.filters.max_file_size = '10mb';
  32:              }
  33:          }
  34:          cfg.pluploadConfig.browse_button = cfg.browseButton.getEl().dom.id;
  35:          me.uploader = new plupload.Uploader(cfg.pluploadConfig);
  36:          me.uploader.init();
  37:      }
  38:  });

使用上也非常简单:

   1:  Ext.create('Ext.ux.Plupload', {
   2:      browseButton: btn,
   3:      pluploadConfig:{ ... }
   4:  });

btn就是要绑定上传动作的Extjs的Button,pluploadConfig就是Plupload的配置参数。

扩展一个PluploadButton

上面简单的使用方法需要btn已经渲染出来,同时如果btn被destroy需要同时把uploader也destroy掉,那就扩展一个Button:

   1:  Ext.define('Ext.ux.button.PluploadButton', {
   2:      extend: 'Ext.Button',
   3:      alias: ['widget.pluploadbutton'],
   4:      constructor: function (config) {
   5:          var me = this;
   6:          me.callParent(arguments);
   7:          me.on('render', function (btn, eOpts) {
   8:              btn.uploader = Ext.create('Ext.ux.Plupload', {
   9:                  browseButton: btn,
  10:                  pluploadConfig: eOpts.pluploadConfig
  11:              });
  12:          }, me, { pluploadConfig: config.pluploadConfig });
  13:          me.on('destroy', function (btn, eOpts) {
  14:              if (btn.uploader) {
  15:                  btn.uploader.uploader.destroy();
  16:              }
  17:          })
  18:      }
  19:  });

现在直接使用pluploadbutton就可以了:

   1:  {
   2:      xtype: 'pluploadbutton',
   3:      text: '上传图片',
   4:      pluploadConfig: {
   5:          url: '/Images/Upload',
   6:          flash_swf_url: '/Scripts/plupload/js/Moxie.swf',
   7:          filters: {
   8:              mime_types: [
   9:                  {
  10:                      title: "图片文件",
  11:                      extensions: "jpg,gif,png"
  12:                  }
  13:              ]
  14:          },
  15:          init: {
  16:              QueueChanged: function (up) {
  17:                //单文件上传
  18:                  up.start();
  19:              },
  20:              FileUploaded: function (up, files, response) {
  21:                  var json = Ext.JSON.decode(response.response);
  22:                 .........
  23:   
  24:              }
  25:          }
  26:      }
  27:  }

这个例子只要选择了一个文件就开始上传,你可以根据官方的文档做一个Extjs的文件队列,把文件选择到队列中,然后点击上传按钮在统一上传。