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

比如说,他可以实现在添加一个文件后为文件添加一个上传按钮,这个是写在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. 解决 mysql 数据库 挂掉了

    问题 : mysql运行几天之后就挂掉了 , 修改了mysql 的连接数也解决不了,看代码也没有什么问题,但就是感觉哪个功能一直占着mysql资源,查了一下当前的线程状态 time的单位是 秒 , 可 ...

  2. 11_java之接口和多态

    01接口的概念 * A:接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来 ...

  3. 自动化装配Bean

    一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...

  4. Python开发丨这些面试题会不会难倒你

    1:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object):       x = 1   class Child1(Parent):       pass   cl ...

  5. Hibernate面试问题集锦: 概述

    ImportNew注: 本文是ImportNew编译整理的Java面试题系列文章之一.你可以从这里查看全部的Java面试系列. Q.怎么配置Hibernate? A.Configuration类使用配 ...

  6. 【313】python 中 print 函数用法总结

    参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...

  7. android学习-Eclipse中修改Android项目图标

    参考原文:http://blog.csdn.net/wpwbb510582246/article/details/52556753 方法一:替换res文件夹下的ic_launcher-web.png图 ...

  8. <mvc:annotation-driven />讲解

    <mvc:annotation-driven />是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学者快速应用默认配置方案.<mvc:annotation-driv ...

  9. css实现栅格的方法

    1. 方法一 1.1. 效果 2. 方法二 2.1. 效果 3. 代码 3.1. Html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...

  10. word2012写论文之参考文献和图片

    每次写论文都感到word的“博大精深”啊,一个版本一个样,一些原来版本还有的功能,后来的版本就没有了,一些原来版本叫这个名字的功能,下一个版本就换了个名字,你也太考验你用户的智商和耐心了吧!即使同一份 ...