文件上传下载(四) 读 txt 文本 ajaxfileupload
前台,使用 ajaxfileupload.js 插件
插件:代码
jQuery.extend({ // create frame createUploadIframe : function(id, uri) { var frameId = 'jUploadFrame' + id; if (window.ActiveXObject) { (function(jQuery){ if(jQuery.browser) return; jQuery.browser = {}; jQuery.browser.mozilla = false; jQuery.browser.webkit = false; jQuery.browser.opera = false; jQuery.browser.msie = false; var nAgt = navigator.userAgent; jQuery.browser.name = navigator.appName; jQuery.browser.fullVersion = ''+parseFloat(navigator.appVersion); jQuery.browser.majorVersion = parseInt(navigator.appVersion,10); var nameOffset,verOffset,ix; // In Opera, the true version is after "Opera" or after "Version" if ((verOffset=nAgt.indexOf("Opera"))!=-1) { jQuery.browser.opera = true; jQuery.browser.name = "Opera"; jQuery.browser.fullVersion = nAgt.substring(verOffset+6); if ((verOffset=nAgt.indexOf("Version"))!=-1) jQuery.browser.fullVersion = nAgt.substring(verOffset+8); } // In MSIE, the true version is after "MSIE" in userAgent else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) { jQuery.browser.msie = true; jQuery.browser.name = "Microsoft Internet Explorer"; jQuery.browser.fullVersion = nAgt.substring(verOffset+5); } // In Chrome, the true version is after "Chrome" else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) { jQuery.browser.webkit = true; jQuery.browser.name = "Chrome"; jQuery.browser.fullVersion = nAgt.substring(verOffset+7); } // In Safari, the true version is after "Safari" or after "Version" else if ((verOffset=nAgt.indexOf("Safari"))!=-1) { jQuery.browser.webkit = true; jQuery.browser.name = "Safari"; jQuery.browser.fullVersion = nAgt.substring(verOffset+7); if ((verOffset=nAgt.indexOf("Version"))!=-1) jQuery.browser.fullVersion = nAgt.substring(verOffset+8); } // In Firefox, the true version is after "Firefox" else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) { jQuery.browser.mozilla = true; jQuery.browser.name = "Firefox"; jQuery.browser.fullVersion = nAgt.substring(verOffset+8); } // In most other browsers, "name/version" is at the end of userAgent else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) { jQuery.browser.name = nAgt.substring(nameOffset,verOffset); jQuery.browser.fullVersion = nAgt.substring(verOffset+1); if (jQuery.browser.name.toLowerCase()==jQuery.browser.name.toUpperCase()) { jQuery.browser.name = navigator.appName; } } // trim the fullVersion string at semicolon/space if present if ((ix=jQuery.browser.fullVersion.indexOf(";"))!=-1) jQuery.browser.fullVersion=jQuery.browser.fullVersion.substring(0,ix); if ((ix=jQuery.browser.fullVersion.indexOf(" "))!=-1) jQuery.browser.fullVersion=jQuery.browser.fullVersion.substring(0,ix); jQuery.browser.majorVersion = parseInt(''+jQuery.browser.fullVersion,10); if (isNaN(jQuery.browser.majorVersion)) { jQuery.browser.fullVersion = ''+parseFloat(navigator.appVersion); jQuery.browser.majorVersion = parseInt(navigator.appVersion,10); } jQuery.browser.version = jQuery.browser.majorVersion; })(jQuery); if(jQuery.browser.version == "9.0"){ var io = document.createElement('iframe'); io.id = frameId; io.name = frameId; }else{ 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; }, // create form createUploadForm : function(id, fileElementId, paramData) { 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(paramData){ for(var paramName in paramData){ $('<input type="hidden" name="' + paramName + '" value="' + paramData[paramName] + '" />').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 = s.fileElementId; var form = jQuery.createUploadForm(id, s.fileElementId, s.data); var io = jQuery.createUploadIframe(id, s.secureuri); var frameId = 'jUploadFrame' + id; var formId = 'jUploadForm' + id; if (s.global && !jQuery.active++) { // Watch for a new set of requests jQuery.event.trigger("ajaxStart"); } var requestDone = false; // Create the request object var xml = {}; if (s.global) { jQuery.event.trigger("ajaxSend", [ xml, s ]); } var uploadCallback = function(isTimeout) { // Wait for a response to come back 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) { //alert("catch->"+e); //jQuery.handleError(s, xml, null, e); if(s.error){ s.error( s.context || s, xml, "error", 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 (s.success) { // ifa local callback was specified, fire it // and pass it the data s.success(data, status); } if (s.global) { // Fire the global callback jQuery.event.trigger("ajaxSuccess", [ xml, s ]); } } else { //jQuery.handleError(s, xml, status); if(s.error){ s.error( s.context || s, xml, status); } } } catch (e) { //alert("error->"+e); status = "error"; //jQuery.handleError(s, xml, status, e); if(s.error){ s.error( s.context || s, xml, status, e); } } if (s.global) { // The request was completed jQuery.event.trigger("ajaxComplete", [ xml, s ]); } // Handle the global AJAX counter if (s.global && !--jQuery.active) { jQuery.event.trigger("ajaxStop"); } // 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); if(s.error){ s.error( s.context || s, xml, "error", status); } } }, 100); xml = null; } }; // Timeout checker if (s.timeout > 0) { setTimeout(function() { if (!requestDone) { // Check to see ifthe request is still happening 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) { form.encoding = 'multipart/form-data'; } else { form.enctype = 'multipart/form-data'; } jQuery(form).submit(); //alert("submit"); } catch (e) { //alert("submit error->"+e.message); //jQuery.handleError(s, xml, null, e); if(s.error){ //s.error.call( s.context || s, xhr, status, e ); s.error( s.context || s, xml, "error", e ); } } /* if (window.attachEvent) { document.getElementById(frameId).attachEvent('onload', uploadCallback); } else { document.getElementById(frameId).addEventListener('load', uploadCallback, false); } */ jQuery('#' + frameId).load(uploadCallback); return { abort : function() { } }; }, uploadHttpData : function(r, type) { var data = !type; data = type == "xml" || data ? r.responseXML : r.responseText; // ifthe type is "script", eval it in global context if (type == "script") { jQuery.globalEval(data); } // Get the JavaScript object, ifJSON is used. if (type == "json") { if(data){ var preBeginIndex = data.indexOf("<pre"); if(preBeginIndex != -1){ var preEndIndex = data.indexOf(">", preBeginIndex); var preText = data.substring(preBeginIndex, preEndIndex+1); //alert(preBeginIndex + "==" + preEndIndex + "==" + preText); data = data.replace(preText, "").replace("</pre>", ""); } } var tagIndex = data.indexOf("{"); if(tagIndex > 0){ data = "{"+data+"}"; } eval("data = " + data); //data = jQuery.parseJSON("data = " + data); } // evaluate scripts within html if (type == "html") { jQuery("<div>").html(data).evalScripts(); } return data; } });
## 注意:1.不要放在submit 中,不然会导致无法执行回调操作,可以用onclick 事件触发。2.先引 jquery.min.js 然后再引 ajaxfileupload.js
html:
<div class="layui-form-item magt3"> <label class="layui-form-label">请选择要导入文件</label> <div class="layui-input-block"> <input type="file" id="filePath" name="filePath" style="margin-top: 5px;"/> </div> </div>
js:
$.ajaxFileUpload({ url: xxxx+'/xx/xxx', secureuri: false, type: 'post', fileElementId: "filePath",// 这里写自己的 dataType: "json", data: {hehe:$("#hehe").val()}, success: function(data){ if (data.success) { // 成功提示语 // layui 的关窗 parent.layer.close(parent.layer.getFrameIndex(window.name)); }else{ // 失败提示语 } } });
后台接收:用 HttpServletRequest request 即可
serviceImpl:
@Override public void importTxt(HttpServletRequest request,String hehe){ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile multipartFile = multipartRequest.getFile("filePath"); if (multipartFile != null) { InputStream is = null; try { is = multipartFile.getInputStream(); } catch (IOException e) { e.printStackTrace(); } //获取表中数据 List<String> dataList = 方法1(is); // 为空判断 if (dataList == null || dataList.size() == 0) { throw new ServiceException(400,"导入失败:没有可以导入的数据!"); } // 后续代码 } }
方法1:
/**获取txt的信息*/ public List<String> importTxt(InputStream inputStream) { List<String> stringList = new ArrayList<>(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line = null; try { while ((line = reader.readLine()) != null) { stringList.add(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return stringList; } }
### 后续可能用到的 String [] arr = str.split("\\s+"); 用空格分隔字符
-------博客内容仅用于个人学习总结-------