这个控件功能还不错,但是对于外部调用上传有点压力。

比如说,他可以实现在添加一个文件后为文件添加一个上传按钮,这个是写在add中的,可以调用data.submit()提交数据

但是如果我要实现外部按钮点击时触发这个data.submit()就有压力了。

有两种方式:

1、想办法保存data,在外部事件触发时,调用到这个data并submit。

2、想办法为该外部按钮在add时注册一个事件,并在点击时触发这个事件后submit()

3、生成一个隐藏button,在外部事件触发时,调用这个button.click()即可。但是要注意重复绑定的问题,要先清除一下事件。

(典型应用,一个表单中有一个上传输入框,我想调用这个上传输入框的进度事件,用默认的提交有点不方便,就需要使用这个控件,但是在使用控件后,提交时就会因为控件的原因,要么提交后取不到文件(加入add,submit等之后),要么能上传,但是进度无事件(不加任何属性))。

现在的要求是,点击外部上传按钮,调用这个控件来上传。

研究upload-ui源码后,发现他是调用了jQuery.data来保存对象,相当于第一种方法:

add: function (e, data) {
if (e.isDefaultPrevented()) {
return false;
}
var $this = $(this),
that = $this.data('blueimp-fileupload') ||
$this.data('fileupload'),
options = that.options;
data.context = that._renderUpload(data.files)
.data('data', data) //这里把数据绑定进去
.addClass('processing');
options.filesContainer[
options.prependFiles ? 'prepend' : 'append'
](data.context);
that._forceReflow(data.context);
that._transition(data.context);
data.process(function () {
return $this.fileupload('process', data);
}).always(function () {
data.context.each(function (index) {
$(this).find('.size').text(
that._formatFileSize(data.files[index].size)
);
}).removeClass('processing');
that._renderPreviews(data);
}).done(function () {
data.context.find('.start').prop('disabled', false);
if ((that._trigger('added', e, data) !== false) &&
(options.autoUpload || data.autoUpload) &&
data.autoUpload !== false) {
data.submit();
}
}).fail(function () {
if (data.files.error) {
data.context.each(function (index) {
var error = data.files[index].error;
if (error) {
$(this).find('.error').text(error);
}
});
}
});
},

这里绑定事件

 _initEventHandlers: function () {
this._super();
this._on(this.options.filesContainer, {
'click .start': this._startHandler,
'click .cancel': this._cancelHandler,
'click .delete': this._deleteHandler
});
this._initButtonBarEventHandlers();
},

这里是事件:

_startHandler: function (e) {
e.preventDefault();
var button = $(e.currentTarget),
template = button.closest('.template-upload'),
data = template.data('data');
button.prop('disabled', true);
if (data && data.submit) {
data.submit();
}
},

取到其中的data并提交。

其实也可以调用send方法来发送,但是send方法的参数要求较高,

$('#fileupload').fileupload('send', {files: filesList});

要求参数filesList为 an object with an array (or array-like list) of File or Blobobjects as files property.

同样添加时也可以直接添加文件列表,并可同时覆盖一部分属性

$('#fileupload').fileupload('add', {files: filesList, url: '/path/to/upload/handler.json'});
Retrieving overall progress data
var overallProgress = $('#fileupload').fileupload('progress');

jQuery-File-Upload外部按钮触发上传的更多相关文章

  1. 用jQuery File Upload做的上传控件demo,支持同页面多个上传按钮

    需求 有这么一个需求,一个form有多个文件要上传,但又不是传统的图片批量上传那种,是类似下图这种需求,一开始是用的swfupload做的上传,但是问题是如果有多个按钮的话,就要写很多重复的代码,于为 ...

  2. 定制jQuery File Upload为微博式单文件上传

    日志未经声明,均为AlloVince原创.版权采用『 知识共享署名-非商业性使用 2.5 许可协议』进行许可. jQuery File Upload是一个非常优秀的上传组件,主要使用了XHR作为上传方 ...

  3. jQuery File Upload文件上传插件简单使用

    前言 开发过程中有时候需要用户在前段上传图片信息,我们通常可以使用form标签设置enctype=”multipart/form-data” 属性上传图片,当我们点击submit按钮的时候,图片信息就 ...

  4. jQuery File Upload跨域上传

    最近在做一个一手粮互联网项目,方案为前后端分离,自己负责前端框架,采用了Requirejs+avalonjs+jquery三个框架完成. 前后端通过跨域实现接口调用,中间也发现了不少问题,尤其是在富文 ...

  5. jQuery File Upload 图片上传解决方案兼容IE6+

    1.下载:https://github.com/blueimp/jQuery-File-Upload 2.命令: npm install bower install ================= ...

  6. jQuery File Upload 文件上传插件使用一 (最小安装 基本版)

    jQuery File Upload 是一款非常强大的文件上传处理插件,支持多文件上传,拖拽上传,进度条,文件验证及图片音视频预览,跨域上传等等. 可以说你能想到的功能它都有.你没想到的功能它也有.. ...

  7. jquery file upload + asp.net 异步多文件上传

    百度了很久,国内一直 找不到 使用jquery file upload 插件 +asp.net 的相关代码 一开始使用 jquery uploadify ,一款基于 flash的插件,但是不支持 Sa ...

  8. jquery file upload 文件上传插件

    1. jquery file upload 下载 jquery file upload Demo 地址:https://blueimp.github.io/jQuery-File-Upload/ jq ...

  9. web文件上传组件比较jQuery File Upload和Fine Uploader

    jQuery File Upload: https://blueimp.github.io/jQuery-File-Upload/ Fine Uploader: http://fineuploader ...

随机推荐

  1. Tomcat实践

    1.1Java环境介绍 jdk  java 开发工具包 jre sdk J2EE 企业版 J2SE 标准版 J2ME 手机开发 1.2Tomcat自动部署 通过saltstack来批量安装tomcat ...

  2. Vue之cookie操作(原生)

    Vue之cookie操作(原生) 再vue组件中加入以下几个方法,然后调用即可. methods:{ //读取cookie,需要注意的是cookie是不能存中文的,如果需要存中文,解决方法是后端先进行 ...

  3. .net Reactor之限指定设备使用

    .net Reactor之license限指定设备使用 上一篇(https://www.cnblogs.com/s313139232/p/9908400.html)中记录了.net Reactor对d ...

  4. Linux下的service命令和chkconfig命令的原理

    CentOS下的service命令和chkconfig命令的原理 1.service命令的原理 service命令用来对服务进行启动和关闭,比如service mysqld start可以启动mysq ...

  5. mysql数据库怎么安装到自己的电脑上?

    安装mysql1.安装过程中输⼊入Y确认继续安装 2.设置密码1.第⼀一次设登陆密码 2.第⼆二次验证密码 安装完成

  6. 104. Maximum Depth of Binary Tree (Tree; DFS)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  7. Unity中Avatar换装实现

    http://www.cnblogs.com/herenzhiming/articles/6533162.html

  8. code4511 信息传递

    寻找最小环 #include <cstdio> #include <cstring> #include <iostream> using namespace std ...

  9. tp5 $this/Db

  10. stristr函数