异步上传图片-不压缩图片
来源:互联网 发布:数控车床编程文本教程 编辑:程序博客网 时间:2024/04/30 03:54
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸为1MB --> <property name="maxUploadSize"> <value>10485760</value> </property> </bean>
spring.xml
<form action="#" name="myform" method="post"> <input type='file' id="uploada" name="uploada" size='50'/> <input type="button" style="width: 5em" onclick="uploadImages('uploada')" value='上传'/> <input type='file' id="uploadb" name="uploadb" size='50'/> <input type="button" style="width: 5em" onclick="uploadImages('uploadb')" value='上传'/> <input type='file' id="uploadc" name="uploadc" size='50'/> <input type="button" style="width: 5em" onclick="uploadImages('uploadc')" value='上传'/> </form>html
function uploadImages(type) {$.ajaxFileUpload({//跟具updateP得到不同的上传文本的ID url:'${ctx}/pages/Demo/uploadImg.htm?updateP='+type, //需要链接到服务器地址 secureuri:false, fileElementId:''+type+'', //文件选择框的id属性(必须) dataType: 'text', success: function (data, status){ var data = eval("("+data+")");if(data.updateP=='uploada'){alert("${ctx}/upload/"+data.fileName);}else if(data.updateP=='uploadb'){alert("${ctx}/upload/"+data.fileName);}else if(data.updateP=='uploadc'){alert("${ctx}/upload/"+data.fileName);} }, error: function (data, status, e){ alert('上传失败'); } } );}JS
public ModelAndView uploadImg(HttpServletRequest request, HttpServletResponse response) throws IOException{request.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();SimpleDateFormat simpleFormat = new SimpleDateFormat("MMddHHmmsss");String generationfileName = simpleFormat.format(new Date())+new Random().nextInt(1000);//保存路径String savePath = "D:/upload";String updateP = request.getParameter("updateP");String fileNameSuffix=null;String fileName= null;if(null!=updateP&&!"".equals(updateP)){try {MultipartFile mf = ((MultipartRequest) request).getFile(updateP);fileName=mf.getOriginalFilename();if(null!=mf&&!"".equals(mf)){fileNameSuffix=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());SaveFileFromInputStream(mf.getInputStream(),savePath,generationfileName+"."+fileNameSuffix);out.write("{'state':'0','fileName':'"+generationfileName+"."+fileNameSuffix+"','updateP':'"+updateP+"'}");}} catch (Exception e) {out.write("{'state':'1'}");e.printStackTrace();}}return null;}//保存文件 1,文件 2,保存路径 3,文件名称public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException{ FileOutputStream fs=new FileOutputStream( path + "/"+ filename); byte[] buffer =new byte[1024*1024]; int bytesum = 0; int byteread = 0; while ((byteread=stream.read(buffer))!=-1){ bytesum+=byteread; fs.write(buffer,0,byteread); fs.flush(); } fs.close(); stream.close(); }JAVA
// JavaScript DocumentjQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId = 'jUploadFrame' + id; if(window.ActiveXObject) { if($.browser.version=="9.0" || $.browser.version=="10.0"){ var io = document.createElement('iframe'); io.id = frameId; io.name = frameId; }else if($.browser.version=="6.0" || $.browser.version=="7.0" || $.browser.version=="8.0"){ 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) { //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>'); 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); 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) { 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( 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); } } catch(e) { status = "error"; jQuery.handleError(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); } }, 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(); } catch(e) { jQuery.handleError(s, xml, null, e); } if(window.attachEvent){ document.getElementById(frameId).attachEvent('onload', 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; // 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" ) { eval( "data = " + data ); } // evaluate scripts within html if( type == "html" ) { jQuery("<div>").html(data).evalScripts(); } return data; }});
ajaxfileupload.js
阅读全文
0 0
- 异步上传图片-不压缩图片
- 前端压缩图片+ajax+php 异步上传
- javascript异步上传压缩图片并立即显示图片
- 图片压缩上传 上传 压缩
- 使用lrz实现客户端异步图片压缩上传
- 上传图片按比例压缩
- 图片上传压缩处理
- 图片上传之压缩
- 图片压缩上传
- 图片压缩上传
- 图片上传压缩处理
- android图片压缩上传
- 图片上传之压缩
- 图片上传之压缩
- Android 图片压缩上传
- 图片压缩 ios 上传
- 图片上传压缩
- 图片上传压缩处理
- Java-NIO(四):通道(Channel)的原理与获取
- 从虚拟机中下载文件到本地
- 11_等待/通知(wait/notify)机制介绍
- 使用MockMVC测试控制器【转】
- SD卡文件权限
- 异步上传图片-不压缩图片
- 进程调度API之init_waitqueue_head
- HDU 3974 Assign the task (并查集&线段树)
- 如何登陆服务器
- TCP三次握手wireshark抓包分析
- 数据结构-抽象数据类型
- php技能树
- Linux系统下强制踢掉登录用户
- Wget用法、参数解释的比较好的一个文章