// JavaScript Document // prepare the form when the DOM is ready $(document).ready(function() { var show = function(data){ alet('ffff'); alert(data); } // bind to the form's submit event $('#upload').ajaxForm({ target:'#uploadMsg', dataType: 'json', beforeSubmit:showRequest, success:showResponse }); }); // pre-submit callback function showRequest(formData, jqForm) { alert('beforeSubmit'); // formData is an array; here we use $.param to convert it to a string to display it // but the form plugin does this for you automatically when it submits the data //var queryString = $.param(formData); // jqForm is a jQuery object encapsulating the form element. To access the // DOM element for the form do this: for (var i=0; i < formData.length; i++) { if (!formData[i].value) { alert('Please select a file'); return false; } } //alert('About to submit: \n\n' + queryString); // here we could return false to prevent the form from being submitted; // returning anything other than false will allow the form submit to continue return true; } // post-submit callback function showResponse(data) { // for normal html responses, the first argument to the success callback // is the XMLHttpRequest object's responseText property // if the ajaxSubmit method was passed an Options Object with the dataType // property set to 'xml' then the first argument to the success callback // is the XMLHttpRequest object's responseXML property // if the ajaxSubmit method was passed an Options Object with the dataType // property set to 'json' then the first argument to the success callback // is the json data object returned by the server alert('success'); alert(data); }
服务器端用的是struts2.1,一个uploadAction
现在能实现上传
页面代码如下:
<div class="upload"> <s:form id="upload" action ="ajax/upload!topicUpload.do" method="post" enctype="multipart/form-data"> <div><input id="topicId" name="topicId" type="hidden" value="<s:property value="topicId"/>"/></div> <div><input id="type" name="type" type="hidden" value="2"/></div> <div><label class="adminleft">附件:</label><s:file name ="myFile" label ="upload File" /> <span class="loading">...</span><s:submit value="上传"/> </div> </s:form> <div class="fileUrl"></div> <div><label id="uploadMsg">你还没有上传任何文件</label></div> </div> </div>
jquery用的是1.3,form插件是最新的,文件上传后,返回的一个upload!topicUpload.do的json文件,还让你保存,就是不执行回调函数,实在搞不懂了,
struts的配置
<!-- ajax模块 --> <package name="ajax" extends="json-default"> <action name="upload" class="uploadAction"> <interceptor-ref name="fileUpload"> <param name="maximumSize">20480000</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> <result type="json"> </result> </action> </package>
不知道有没有谁也遇到过这样的问题