- 浏览: 52898 次
- 性别:
- 来自: 北京
最新评论
-
jusfeel:
第一个serialize很有用。
两个jquery插件:序列化表单,处理checkbox全选 -
wenjundiandian:
jQuery.uniqueArray 未定义。。。
jQuery HashTable -
wenjundiandian:
果断大牛。。。
jQuery HashTable
原址:http://www.phpletter.com。修改了一个不支持高版本jquery的bug
ajaxfileupload.js:
test.js
test.html
ajaxfileupload.js:
jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId = 'jUploadFrame' + id; var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"'; if(window.ActiveXObject) { if(typeof uri== 'boolean'){ iframeHtml += ' src="' + 'javascript:false' + '"'; } else if(typeof uri== 'string'){ iframeHtml += ' src="' + uri + '"'; } } iframeHtml += ' />'; jQuery(iframeHtml).appendTo(document.body); return jQuery('#' + frameId).get(0); }, createUploadForm: function(id, fileElementId, data) { //create form var formId = 'jUploadForm' + id; var fileId = 'jUploadFile' + id; var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>'); if(data) { for(var i in data) { jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form); } } var oldElement = jQuery('#' + fileElementId); var newElement = jQuery(oldElement).clone(); jQuery(oldElement).attr('id', fileId); jQuery(oldElement).before(newElement); jQuery(oldElement).appendTo(form); //set attributes jQuery(form).css('position', 'absolute'); jQuery(form).css('top', '-1200px'); jQuery(form).css('left', '-1200px'); jQuery(form).appendTo('body'); return form; }, ajaxFileUpload: function(s) { // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout s = jQuery.extend({}, jQuery.ajaxSettings, s); var id = new Date().getTime() var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data)=='undefined'?false:s.data)); var io = jQuery.createUploadIframe(id, s.secureuri); var frameId = 'jUploadFrame' + id; var formId = 'jUploadForm' + id; // Watch for a new set of requests if ( s.global && ! jQuery.active++ ) { jQuery.event.trigger( "ajaxStart" ); } var requestDone = false; // Create the request object var xml = {} if ( s.global ) jQuery.event.trigger("ajaxSend", [xml, s]); // Wait for a response to come back var uploadCallback = function(isTimeout) { var io = document.getElementById(frameId); try { if(io.contentWindow) { xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null; xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document; }else if(io.contentDocument) { xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null; xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document; } }catch(e) { jQuery.handleError(s, xml, null, e); } if ( xml || isTimeout == "timeout") { requestDone = true; var status; try { status = isTimeout != "timeout" ? "success" : "error"; // Make sure that the request was successful or notmodified if ( status != "error" ) { // process the data (runs the xml through httpData regardless of callback) var data = jQuery.uploadHttpData( xml, s.dataType ); // If a local callback was specified, fire it and pass it the data if ( s.success ) s.success( data, status ); // Fire the global callback if( s.global ) jQuery.event.trigger( "ajaxSuccess", [xml, s] ); } else jQuery.handleError(s, xml, status); } catch(e) { status = "error"; jQuery.handleError(s, xml, status, e); } // The request was completed if( s.global ) jQuery.event.trigger( "ajaxComplete", [xml, s] ); // Handle the global AJAX counter if ( s.global && ! --jQuery.active ) jQuery.event.trigger( "ajaxStop" ); // Process result if ( s.complete ) s.complete(xml, status); jQuery(io).unbind() setTimeout(function() { try { jQuery(io).remove(); jQuery(form).remove(); } catch(e) { jQuery.handleError(s, xml, null, e); } }, 100) xml = null } } // Timeout checker if ( s.timeout > 0 ) { setTimeout(function(){ // Check to see if the request is still happening if( !requestDone ) uploadCallback( "timeout" ); }, s.timeout); } try { var form = jQuery('#' + formId); jQuery(form).attr('action', s.url); jQuery(form).attr('method', 'POST'); jQuery(form).attr('target', frameId); if(form.encoding) { jQuery(form).attr('encoding', 'multipart/form-data'); } else { jQuery(form).attr('enctype', 'multipart/form-data'); } jQuery(form).submit(); } catch(e) { jQuery.handleError(s, xml, null, e); } jQuery('#' + frameId).load(uploadCallback ); return {abort: function () {}}; }, uploadHttpData: function( r, type ) { var data = !type; data = type == "xml" || data ? r.responseXML : r.responseText; // If the type is "script", eval it in global context if ( type == "script" ) jQuery.globalEval( data ); // Get the JavaScript object, if JSON is used. if ( type == "json" ) eval( "data = " + data ); // evaluate scripts within html if ( type == "html" ) jQuery("<div>").html(data).evalScripts(); return data; }, handleError: function( s, xhr, status, e ) { // If a local callback was specified, fire it if ( s.error ) { s.error.call( s.context || s, xhr, status, e ); } // Fire the global callback if ( s.global ) { (s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] ); } } })
test.js
function ajaxFileUpload() { $("#loading") .ajaxStart(function(){ $(this).show(); }) .ajaxComplete(function(){ $(this).hide(); }); $.ajaxFileUpload ( { url:'/Shopping/t', secureuri:false, fileElementId:'fileToUpload', dataType: 'json', data:{name:'logan', id:'id'}, success: function (data, status) { alert(data.msg); if(typeof(data.error) != 'undefined') { if(data.error != '') { alert(data.error); }else { alert(data.msg); } } }, error: function (data, status, e) { alert(e); } } ) return false; }
test.html
<html> <head> <title>Ajax File Uploader Plugin For Jquery</title> <script type="text/javascript" src="../static/js/jquery-1.6.1.js"></script> <script type="text/javascript" src="/static/js/ajaxfileupload.js"></script> <script type="text/javascript" src="/static/test.js"></script> </head> <body> <div id="wrapper"> <div id="content"> <img id="loading" src="loading.gif" style="display:none;"> <form name="form" action="" method="POST" enctype="multipart/form-data"> <table cellpadding="0" cellspacing="0" class="tableForm"> <thead> <tr> <th>Please select a file and click Upload button</th> </tr> </thead> <tbody> <tr> <td><input id="fileToUpload" type="file" size="45" name="fileToUpload" class="input"></td> </tr> </tbody> <tfoot> <tr> <td><button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">Upload</button></td> </tr> </tfoot> </table> </form> </div> </body> </html>
发表评论
-
js格式化日期
2013-07-01 17:24 630/** * 对Date的扩展,将 Date 转化为 ... -
jQuery ajax设置全局错误处理
2013-06-05 17:15 18710$(function(){ // 设置jQuery A ... -
JS类似Java String.format的函数
2013-01-16 15:30 5868String.prototype.format = Str ... -
javascript 阻止事件冒泡
2012-08-28 10:16 781function eventStopBubble() { ... -
两个jquery插件:序列化表单,处理checkbox全选
2012-08-23 17:03 9275将表单数据转换成JSON对象,处理一组checkbox全选,全 ... -
javascript中小数取整的方法总结:
2012-03-01 10:21 1169javascript中小数取整的方法总结: 以下总结了在js中 ... -
Javascript 格式化货币
2012-02-06 10:36 1712/** 货币格式化函数 **/ function f ... -
jQuey List (unique)
2012-01-09 10:09 737jQuery.uniqueArray = function ... -
jQuery HashTable
2012-01-09 10:07 3701jQuery.hashTable = function ( ... -
JavaScript 四舍五入
2011-12-21 07:50 613var up = 500.125 up = Math.r ... -
jQuery的几个常用东西,,,
2011-11-16 11:51 723AJAX $.ajax({ type:"P ...
相关推荐
最初认为用ajax上传文件是件很困难的事,用ajaxfileupload后可以上传任何类型文件,简单好用。
真正实现ajax上传文件 兼容IE6789火狐谷歌世界之窗
jquery实现ajax上传文件asp.net版
上传文件AJAX上传文件AJAX上传文件AJAX上传文件AJAX上传文件AJAX上传文件AJAX
PHP 通过ajax 上传文件的方法,非常好用,并局部刷新上传文件信息
ajax上传文件,使用了jquery.uploadify.js,和dropzone.min.js两种上传方式
asp无刷新上传文件ajax上传文件 请将本程序放到你的虚拟主机上运行,如果本地有iis也可以。有些同学有简易iis服务器,不适用本程序的,因为有部分中文字符不能识别导致无法运行。 如果需要上传多文件可以把index....
Ajax上传文件.pdf Ajax上传文件.pdf
ajax实现的文件上传,状态显示
最全的ajax上传demo,可上传图片,文件等,带文件验证,代码直接复制可用,亲测有效!
这是本人自己做的js+ajax上传文件的代码,点击页面上的图片按钮,选择图片文件,就可直接上传,上传后的文件名称直接保存在隐藏域标签里,方便后台获取。
Ajax上传文件Demo以及Ajax上传文件前的预览效果实现。详情见博客http://blog.csdn.net/u012702547/article/details/77585111
用到了 ajaxfileupload.js + ashx, 其它没什么好说的, 算是留个底吧。
JQuery实现ajax上传文件示例源码
Mvc Ajax 上传文件 Mvc Ajax 上传文件 Mvc Ajax 上传文件 Mvc Ajax 上传文件 资源来自于网络 经过本人处理 欢迎大家交流 QQ574311505
JQuery实现ajax上传文件示例源码 源码描述: url用来指定后台处理的程序,fileElementId指的是文件选择框的ID,dataType用来指定返回的数据格式,支持xml、script、json和html。 返回的json的格式最简单:{error:'...
asp.net+query实现文件ajax上传。
ajax上传文件,ajax实现进度条,及springMvc上传文件实现进度条