jquery.ajaxfileupload.js
jquery.ajaxfileupload.js上传插件,利用iframe提交不刷新页面功能完成。
- /*
- // jQuery Ajax File Uploader
- //
- // @author: Jordan Feldstein <jfeldstein.com>
- //
- // - Ajaxifies an individual <input type="file">
- // - Files are sandboxed. Doesn't matter how many, or where they are, on the page.
- // - Allows for extra parameters to be included with the file
- // - onStart callback can cancel the upload by returning false
- */
- (function($) {
- $.fn.ajaxfileupload = function(options) {
- var settings = {
- params: {},
- action: '',
- onStart: function() { },
- onComplete: function(response) { },
- onCancel: function() { },
- validate_extensions : true,
- valid_extensions : ['gif','png','jpg','jpeg'],
- submit_button : null
- };
- var uploading_file = false;
- if ( options ) {
- $.extend( settings, options );
- }
- // 'this' is a jQuery collection of one or more (hopefully)
- // file elements, but doesn't check for this yet
- return this.each(function() {
- var $element = $(this);
- // Skip elements that are already setup. May replace this
- // with uninit() later, to allow updating that settings
- if($element.data('ajaxUploader-setup') === true) return;
- $element.change(function()
- {
- // since a new image was selected, reset the marker
- uploading_file = false;
- // only update the file from here if we haven't assigned a submit button
- if (settings.submit_button == null)
- {
- upload_file();
- }
- });
- if (settings.submit_button == null)
- {
- // do nothing
- } else
- {
- settings.submit_button.click(function(e)
- {
- // Prevent non-AJAXy submit
- e.preventDefault();
- // only attempt to upload file if we're not uploading
- if (!uploading_file)
- {
- upload_file();
- }
- });
- }
- var upload_file = function()
- {
- if($element.val() == '') return settings.onCancel.apply($element, [settings.params]);
- // make sure extension is valid
- var ext = $element.val().split('.').pop().toLowerCase();
- if(true === settings.validate_extensions && $.inArray(ext, settings.valid_extensions) == -1)
- {
- // Pass back to the user
- settings.onComplete.apply($element, [{status: false, message: 'The select file type is invalid. File must be ' + settings.valid_extensions.join(', ') + '.'}, settings.params]);
- } else
- {
- uploading_file = true;
- // Creates the form, extra inputs and iframe used to
- // submit / upload the file
- wrapElement($element);
- // Call user-supplied (or default) onStart(), setting
- // it's this context to the file DOM element
- var ret = settings.onStart.apply($element, [settings.params]);
- // let onStart have the option to cancel the upload
- if(ret !== false)
- {
- $element.parent('form').submit(function(e) { e.stopPropagation(); }).submit();
- } else {
- uploading_file = false;
- }
- }
- };
- // Mark this element as setup
- $element.data('ajaxUploader-setup', true);
- /*
- // Internal handler that tries to parse the response
- // and clean up after ourselves.
- */
- var handleResponse = function(loadedFrame, element) {
- var response, responseStr = $(loadedFrame).contents().text();
- try {
- //response = $.parseJSON($.trim(responseStr));
- response = JSON.parse(responseStr);
- } catch(e) {
- response = responseStr;
- }
- // Tear-down the wrapper form
- element.siblings().remove();
- element.unwrap();
- uploading_file = false;
- // Pass back to the user
- settings.onComplete.apply(element, [response, settings.params]);
- };
- /*
- // Wraps element in a <form> tag, and inserts hidden inputs for each
- // key:value pair in settings.params so they can be sent along with
- // the upload. Then, creates an iframe that the whole thing is
- // uploaded through.
- */
- var wrapElement = function(element) {
- // Create an iframe to submit through, using a semi-unique ID
- var frame_id = 'ajaxUploader-iframe-' + Math.round(new Date().getTime() / 1000)
- $('body').after('<iframe width="0" height="0" style="display:none;" name="'+frame_id+'" id="'+frame_id+'"/>');
- $('#'+frame_id).get(0).onload = function() {
- handleResponse(this, element);
- };
- // Wrap it in a form
- element.wrap(function() {
- return '<form action="' + settings.action + '" method="POST" enctype="multipart/form-data" target="'+frame_id+'" />'
- })
- // Insert <input type='hidden'>'s for each param
- .before(function() {
- var key, html = '';
- for(key in settings.params) {
- var paramVal = settings.params[key];
- if (typeof paramVal === 'function') {
- paramVal = paramVal();
- }
- html += '<input type="hidden" name="' + key + '" value="' + paramVal + '" />';
- }
- return html;
- });
- }
- });
- }
- })( jQuery )
使用方法
- <input type="file" id="image" name="image">
- <script>
$(document).ready(function(){- $("#image").ajaxfileupload({
- 'action': '/admin/studentStore/uploadImage?time='+new Date().getTime(),
- 'onComplete': function(data) {
- var html ="";
- html += "<em class='del-img' onclick='deleteImg(this);' title='删除'></em>";
- html += "<img style='width:100%;height:100%;' src='"+data.filepath+"'>";
- var name = "img"+$.trim(index);
- html += "<input type='hidden' name="+name+" value='"+data.filepath+"'>";
- $("#img"+index).html(html);
- $("#image"+index).val("");
- $("#image"+index).parents(".form-uploadPic-file-wapper").hide();
- },
- 'onStart': function() {
- },
- 'onCancel': function() {
- },
- valid_extensions:['jpeg','png','gif','jpg']
- });
- });
- </script>
jquery.ajaxfileupload.js的更多相关文章
- Jquery ajaxfileupload.js结合.ashx文件实现无刷新上传
先上几张图更直观展示一下要实现的功能,本功能主要通过Jquery ajaxfileupload.js插件结合ajaxUpFile.ashx一般应用程序处理文件实现Ajax无刷新上传功能,结合NPOI2 ...
- java jfinal + ajaxfileupload.js 上传
功能上传 需求:同时上传多张图片 前端:jquery.ajaxfileupload.js 后端:jfinal upload.htm <html> <body> <div ...
- ajaxfileupload.js插件结合一般处理文件实现Ajax无刷新上传
先上几张图更直观展示一下要实现的功能.本功能主要通过Jquery ajaxfileupload.js插件结合ajaxUpFile.ashx一般应用程序处理文件实现Ajax无刷新上传功能,结合NPOI2 ...
- 引用(ajaxfileupload.js) ajaxfileupload.js报这错jQuery.handleError is not a function
jQuery.handleError is not a function 原因是,经测试handlerError只在jquery-1.4.2之前的版本中存在,jquery-1.6 和1.7中都没有这个 ...
- jQuery插件之上传文件ajaxfileupload.js源码与使用
在网页应用中,一般会用到上传文件或者图片什么的到服务器,那么可以用ajaxfileupload.js,但是在使用ajaxfileupload.js时候,当服务器返回的json带有&符号的时候, ...
- jquery插件--ajaxfileupload.js上传文件原理分析
英文注解应该是原作者写的吧~说实话,有些if判断里的东西我也没太弄明白,但是大致思路还是OK的. jQuery.extend({ createUploadIframe: function (id, u ...
- jQuery 关于IE9上传文件无法进入后台问题的原因及解决办法(ajaxfileupload.js第四弹)
第四弹的诞生完全不在自己最初的计划之中,是有个网友看了先前关于<ajaxfileupload.js系列>的文章后提出的问题,由于自己一直是用chrome浏览器去测试demo,完全忽略IE浏 ...
- jQuery 自制上传头像插件-附带Demo实例(ajaxfileupload.js第三弹)
这篇文章主要是对前两篇关于ajaxfileupload.js插件的文章 <ASP.NET 使用ajaxfileupload.js插件出现上传较大文件失败的解决方法(ajaxfileupload. ...
- jQuery 关于ajaxfileupload.js插件的逐步解析(ajaxfileupload.js第二弹)
如果你看了上一篇<ASP.NET 使用ajaxfileupload.js插件出现上传较大文件失败的解决方法(ajaxfileupload.js第一弹)>的话,应该就知道我是逼不得已要认真学 ...
随机推荐
- Oracle Data Integrator 12c (12.1.2)新特性
改进特性如下: 基于流程界面的声明式设计 在12c中,以前的接口(interface)已经改为映射(mapping),新的基于流程声明的设计方式更灵活,也更容易使用.在12c中,映射的实现是通过使用J ...
- UVALive 4682 XOR Sum (trie)
题意:求一段连续的数字使得它们的异或和最大. 思路:首先利用前缀和求sum[i],这样求某段连续数字异或和最大就是求某两个j和i满足sum[i]^sum[j-1]最大,问题就变成了找两个数的异或最大. ...
- jQuery 关于 end() 方法的详细解释
<ul class="first"> <li class="foo">list item 1</li> <li> ...
- hello iic
刚刚终于弄出来了这个.发现自己很多问题. 一 mian函数 #include "led.h"#include "delay.h"#include "s ...
- redis简介以及与memcached比较
一.redis (1)简介: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.是noSql数据库的一种. re ...
- 认真对待每一道算法题 之 两个排序好的数组寻找的第k个大的数
转载博客:http://www.cnblogs.com/buptLizer/archive/2012/03/31/2427579.html 题目意思:给出两个排好序的数组 ,不妨设为a,b都按升序排列 ...
- 封装定制的Kali Live ISO
打造专属的Kali ISO – 简介 封装定制的Kali ISO很简单,很有趣,很有意义.你可以用Debian的live-build脚本对Kali ISO进行全面的配置.这些脚本以一系列配置文件的方式 ...
- 深入学习:Windows下Git入门教程(下)
声明:由于本人对于Git的学习还处于摸索阶段,对有些概念的理解或许只是我断章取义,有曲解误导的地方还请见谅指正! 一.分支 1.1分支的概念. 对于的分支的理解,我们可以用模块化这个词来解释:在日常工 ...
- PHP版本区别
- html input
disabled="disabled" <input name="" type="checkbox" value="&quo ...