使用Jquery Ajax File Uploader这个控件上传

官方网站:http://www.phpletter.com/DOWNLOAD/

这个控件有个缺点,就是不能传递自定义参数。通过更改ajaxfileupload.js文件可以解决此问题。

主要更改点32行,增加42行的内容,更改56行的内容

32/createUploadForm: function(id, fileElementId)

42/增加文本参数的支持

if (data) {

for (var i in data) {

$('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);

}

}

56/ var form = jQuery.createUploadForm(id, s.fileElementId, s.data);

以下是更改后的文件:

jQuery.extend({

createUploadIframe: function(id, uri)

{

//create frame

var frameId = 'jUploadFrame' + id;

if(window.ActiveXObject) {

var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');

if(typeof uri== 'boolean'){

io.src = 'javascript:false';

}

else if(typeof uri== 'string'){

io.src = uri;

}

}

else {

var io = document.createElement('iframe');

io.id = frameId;

io.name = frameId;

}

io.style.position = 'absolute';

io.style.top = '-1000px';

io.style.left = '-1000px';

document.body.appendChild(io);

return io

},

createUploadForm: function(id, fileElementId, data)

{

//create form

var formId = 'jUploadForm' + id;

var fileId = 'jUploadFile' + id;

var form = $('<form  action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');

var oldElement = $('#' + fileElementId);

var newElement = $(oldElement).clone();

$(oldElement).attr('id', fileId);

$(oldElement).before(newElement);

$(oldElement).appendTo(form);

//增加文本参数的支持

if (data) {

for (var i in data) {

$('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);

}

}

//set attributes

$(form).css('position', 'absolute');

$(form).css('top', '-1200px');

$(form).css('left', '-1200px');

$(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, 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

{

$(io).remove();

$(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 io = $('#' + frameId);

var form = $('#' + formId);

$(form).attr('action', s.url);

$(form).attr('method', 'POST');

$(form).attr('target', frameId);

if(form.encoding)

{

form.encoding = 'multipart/form-data';

}

else

{

form.enctype = 'multipart/form-data';

}

$(form).submit();

} catch(e)

{

jQuery.handleError(s, xml, null, e);

}

if(window.attachEvent){

document.getElementById(frameId).attachEvent(' uploadCallback);

}

else{

document.getElementById(frameId).addEventListener('load', uploadCallback, false);

}

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();

//alert($('param', data).each(function(){alert($(this).attr('value'));}));

return data;

}

})

使用方法如下:

$.ajaxFileUpload({

url: '/ajax/mine/uploadLogo',

secureuri:false,

fileElementId:'input_logo',

dataType: 'json',

data: {//加入的文本参数

"logoPath": "param1",

"logoName": "param2"

},

success: function(data) {

},

error: function() {

showError("上传失败,请检查文件是否符合格式要求。");

}

});

ajaxfileupload带参数上传文件相关推荐

  1. 带上传文件的ajax保存,ajaxfileupload带参数上传文件

    使用Jquery Ajax File Uploader这个控件上传 这个控件有个缺点,就是不能传递自定义参数.通过更改ajaxfileupload.js文件可以解决此问题. 主要更改点32行,增加42 ...

  2. php ajaxfileupload.js 使用,使用ajaxfileupload.js实现上传文件功能

    一直以来上传文件都是使用form表单上传文件,也看到过有人使用js上传文件,不过看起来蛮简单的也就没有怎么去理会.今天突然要使用这种方式上传文件,期间还遇到点问题.因此就记录下来,方便以后遇到这样的问 ...

  3. springmvc + ajaxfileupload 实现异步上传文件(图片)

    最近在做一个项目需要实现异步上传图片,在网上找了很多资料后,采用了ajaxfileupload可以实现,以下是核心代码: jsp: <!-- 上传窗口 --> <div id=&qu ...

  4. 通过ajaxFileUpload异步请求上传文件(ajaxFileUpload+servlet实现文件上传下载)

    1.最终效果 实现选择图片(此处以图片为例,支持所有类型文件的上传),选择之后将选择的图片在页面中显示出来,点击上传,可以将文件上传到指定的地址中,上传成功后在当前页面自动显示下载标签. 源码下载地址 ...

  5. 使用ajaxfileupload.js实现上传文件功能

    一.ajaxFileUpload是一个异步上传文件的jQuery插件. 语法:$.ajaxFileUpload([options]) options参数说明: 1.url 上传处理程序地址 2.fil ...

  6. 一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子

    页面代码: <html>     <!-- 引入相关的js文件,相对路径  -->     <script type="text/javascript" ...

  7. oracle 存储过程 存储 blob,穿越oracle存储过程的Blob参数上传文件

    前两天朋友找我做一个上传过程,极其容易的一个东西!我未曾用过java,现学现卖,反正也是很容易. 不过,其中除非碰到一个东西,在网上查了半晌,也未曾找遍地理的措施!尔后才好不轻率获胜了.我把这个登记下 ...

  8. ajax页面 js文件上传,jQuery插件ajaxfileupload.js实现上传文件

    AjaxUpLoad.js的使用实现无刷新文件上传,如图 1.创建页面并编写HTML 上传文档: 上传图片: 2.引用AjaxUpload.js文件 3.编写JS脚本 window.onload = ...

  9. [原创]使用ajaxFileUpload.js上传文件时附带额外参数。

    最近公司的一个项目涉及到导入Excel的功能,于是就想到用ajaxFileUpload来实现上传文件,因为用过很多次了,网上也有很多文章介绍.使用方法不表.但是在附带参数这个环节卡住了:文件可以上传到 ...

最新文章

  1. Java并发编程71道面试题及答案
  2. 力扣(LeetCode)刷题
  3. 使用 screen 管理你的远程会话
  4. tkinter笔记:通过点击button 控制标签的显示 (莫烦python笔记)
  5. wamp2.2-64位 localhost和localhost/phpmyadmin不能访问问题解决
  6. mycat读写分离部署步骤
  7. jquery点击label触发2次的问题
  8. [文摘20080428]无线局域网的相关网络安全技术应用指南
  9. mysql的分页如何操作_Mysql有关分页的操作
  10. 前后端分离导出excel_Vue + .NetCore前后端分离的快速发开框架
  11. redis应用场景与最佳实践
  12. 易筋SpringBoot 2.1 | 第六篇:JdbcTemplate访问MySQL
  13. 微信 红包 服务器架构,微信红包数据库架构演变.pdf
  14. 迅捷pdf转换器:如何将pdf转换成word
  15. Extracting Multiple-Relations in One-Pass with Pre-Trained Transformers [论文研读]
  16. 小技巧-彻底删除U盘中的文件
  17. 你适不适合做UE交互设计师
  18. 如何将知识结构化,形成知识管理体系(干货分享)
  19. Mybatis中模糊查询的SQL语句应该怎么写?
  20. ERROR: resetting DM9000 -> not responding dm9000 not found at 0x88000000问题解决

热门文章

  1. 当期收益率(Current Yield)
  2. HSSFDataFormat大全
  3. 一个简单的DWR入门例子
  4. Linux中 oracle SQL*PLUS 命令大全
  5. 【新手提问导读】提问的艺术
  6. 【转】C#获取当前路径7种方法
  7. 《Windows Phone 8 Development Internals》读书笔记-1-2-1-连载
  8. javascript谜题
  9. SQLite.NET.0.17 的离奇之处, BUG??? BY DESIGN??
  10. version control(版本控制)