ajaxfileupload实现单个或多个文件的上传

2024-06-14 16:48

本文主要是介绍ajaxfileupload实现单个或多个文件的上传,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 核心js类库:
    jQuery.extend( {handleError : function(s, xhr, status, e) {if (s.error)s.error(xhr, status, e);else if (xhr.responseText)console.log(xhr.responseText);}
    });
    jQuery.extend( {//创建上传的FramecreateUploadIframe : function(id, uri) {// create framevar 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);},//动态创建上传FormcreateUploadForm : function(id, fileElementId, data) {// create formvar 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);*///多文件上传for(var i in fileElementId){  var oldElement = jQuery('#' + fileElementId[i]);  var newElement = jQuery(oldElement).clone();  jQuery(oldElement).attr('id', fileId);  jQuery(oldElement).before(newElement);  jQuery(oldElement).appendTo(form);  }  //end// set attributesjQuery(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 timeouts = 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 requestsif (s.global && !jQuery.active++) {jQuery.event.trigger("ajaxStart");}var requestDone = false;// Create the request objectvar xml = {};if (s.global)jQuery.event.trigger("ajaxSend", [ xml, s ]);// Wait for a response to come backvar 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// notmodifiedif (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 dataif (s.success)s.success(data, status);// Fire the global callbackif (s.global)jQuery.event.trigger("ajaxSuccess", [ xml, s ]);} elsejQuery.handleError(s, xml, status);} catch (e) {status = "error";jQuery.handleError(s, xml, status, e);}// The request was completedif (s.global)jQuery.event.trigger("ajaxComplete", [ xml, s ]);// Handle the global AJAX counterif (s.global && !--jQuery.active)jQuery.event.trigger("ajaxStop");// Process resultif (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 checkerif (s.timeout > 0) {setTimeout(function() {// Check to see if the request is still happeningif (!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 contextif (type == "script")jQuery.globalEval(data);// Get the JavaScript object, if JSON is used.if (type == "json")eval("data = " + data);// evaluate scripts within htmlif (type == "html")jQuery("<div>").html(data).evalScripts();return data;}});
    

  • 使用方法:

  •   //HTML页面标签为file类型的ID
    var files = ['bizLisnScanFile','credScanFile'];	<strong></strong><pre name="code" class="java" style="display: inline !important; ">var params = serializeObject($('#formcompany'));
     
    $.ajaxFileUpload({url: 'goxxxx',secureuri :false,fileElementId :files,//上传文件IDdata: params,//表单值dataType : 'text', error:function(){window.location.href="500";//跳转到500页面},success: function (result,status) {}


这篇关于ajaxfileupload实现单个或多个文件的上传的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1060967

相关文章

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

VSCode设置python SDK路径的实现步骤

《VSCode设置pythonSDK路径的实现步骤》本文主要介绍了VSCode设置pythonSDK路径的实现步骤,包括命令面板切换、settings.json配置、环境变量及虚拟环境处理,具有一定... 目录一、通过命令面板快速切换(推荐方法)二、通过 settings.json 配置(项目级/全局)三、

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

java中BigDecimal里面的subtract函数介绍及实现方法

《java中BigDecimal里面的subtract函数介绍及实现方法》在Java中实现减法操作需要根据数据类型选择不同方法,主要分为数值型减法和字符串减法两种场景,本文给大家介绍java中BigD... 目录Java中BigDecimal里面的subtract函数的意思?一、数值型减法(高精度计算)1.

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

C/C++中OpenCV 矩阵运算的实现

《C/C++中OpenCV矩阵运算的实现》本文主要介绍了C/C++中OpenCV矩阵运算的实现,包括基本算术运算(标量与矩阵)、矩阵乘法、转置、逆矩阵、行列式、迹、范数等操作,感兴趣的可以了解一下... 目录矩阵的创建与初始化创建矩阵访问矩阵元素基本的算术运算 ➕➖✖️➗矩阵与标量运算矩阵与矩阵运算 (逐元

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y