|
|
Spring, ajaxform and Webkit based browsers.
Hello all,
I've been spinning on this problem for many many hours and Im not sure if this is the right place to ask but I figured I better ask somewhere.
I am using the jQuery Form Plugin to upload a file in my spring mvc application. It works fine in every browser except webkit in which the callback is never executed.
Here is the html:
Code:
lt;form id=quot;fq${fn:escapeXml(id)}quot; action=quot;${form_ucl}quot; method=quot;postquot; enctype=quot;multipart/form-dataquot;gt;
File: lt;input type=quot;filequot; name=quot;filequot; size=quot;100quot;/gt;
lt;input type=quot;submitquot; value=quot;Uploadquot; /gt;
lt;input type=quot;hiddenquot; id=quot;id${fn:escapeXml(id)}quot; name=quot;idquot; value=quot;${answer.id}quot;/gt;
lt;input type=quot;hiddenquot; id=quot;ver${fn:escapeXml(id)}quot; name=quot;versionquot; value=quot;${answer.version}quot;/gt;
lt;input type=quot;hiddenquot; id=quot;ques${fn:escapeXml(id)}quot; name=quot;questionIdquot; value=quot;${question.id}quot;/gt;
lt;input type=quot;hiddenquot; name=quot;MAX_FILE_SIZEquot; value=quot;1000000quot; /gt;
lt;div id=quot;uploadOutput${fn:escapeXml(id)}quot;gt;*lt;/divgt;
lt;label class=quot;errorquot;gt;*lt;/labelgt;
lt;/formgt;
And here is the javascript:Code:
$(document).ready(function() {
$('#fq${fn:escapeXml(id)}').ajaxForm({ beforeSubmit: function(a,f,o) { o.dataType = 'json';$('#uploadOutput${fn:escapeXml(id)}').html('Uploading...'); }, success: function(data,textStatus, xhr,form) { var redirectucl = xhr.getResponseHeader('CSR-Redirect-ucl'); if (redirectucl!=null amp;amp; redirectucl.length gt; 0) { window.location = location; return response; } if(data.error!=null){ alert(quot;Received Server Error:quot;+data.error); } /* var $out = $('#uploadMessage${fn:escapeXml(id)}'); */$('#uploadMessage${fn:escapeXml(id)}').html('lt;stronggt;lt;a hrcf=quot;'+data.ucl+'quot; target=quot;_blankquot;gt;' +data.fileName + 'lt;/agt;lt;/stronggt; uploaded successfully******lt;a hrcf=quot;javascript:submitAjaxucl(\'${base_delete_ucl}/'+data.id+'\',\'${fn:escapeXml(id)}\',handleFileDelete);quot;gt;delete(X)lt;/agt;'); handleCommonAnswerAttributes(data,'${fn:escapeXml(id)}',form); $('#uploadOutput${fn:escapeXml(id)}').html('*'); } }); });
After trying to upload a file in Safari or Chrome, the div stays at Loading... and the content is never updated. However a refresh of the page does show the file has been uploaded. I know the server is sending a response because it works fine in firefox, just not sure what the quirk is for webkit. Just in case, here is the controller logic:Code: @RequestMapping(value = quot;/ssc/participate/survey/answerFile/{surveyPid}quot;, method = RequestMethod.POST) public @ResponseBody String create(@PathVariable(quot;surveyPidquot;) Long surveyPid,
@Valid FileAnswer formAnswer, BindingResult result, Model model, fromServletResponse response, fromServletRequest request) throws CSRDataUpdateException, IOException { response.setHeader(quot;Connectionquot;, quot;closequot;);
if (formAnswer == null) throw new IllegalArgumentException(quot;A scriptFile is requiredquot;); if (result.hasErrors()) { throw new CSRDataUpdateException(quot;Error uploading the file try againquot;); } boolean newAnswer=false; FileAnswer answer=null; FileIdentifier existingfileId=null; if(formAnswer.getId()!=null){ answer=FileAnswer.findFileAnswer(formAnswer.getId()); if(answer==null){ return quot;{\quot;error\quot;:\quot;Attempting to update non existing answer\quot;}quot;; } } SurveyParticipance sp=SurveyParticipance.findSurveyParticipance(surveyPid); Long supplierId=sp.getSupplierId(); if(answer==null){ answer =new FileAnswer(sp.getSurveyInstance(),supplierId); answer.prepareNewAnswer( sp, formAnswer); newAnswer=true; }else{ existingfileId=FileIdentifier.createSupplierFileIdentifierWithKey(CSRModules.SSC, supplierId, answer.getFileName(),answer.getFileKey()); answer.setVersion(formAnswer.getVersion()); answer.vefiryAnswer( sp, formAnswer); }
answer.setFile(formAnswer.getFile()); String formFileName=formAnswer.getFileName(); if(formFileName!=null amp;amp; !formFileName.equals(quot;quot;)) answer.setFileName(formFileName); FileIdentifier newfileId=FileIdentifier.createSupplierFileIdentifier(CSRModules.SSC, supplierId, answer.getFileName());; answer.setFileKey(newfileId.getFileKey()); fileService.writeFile(newfileId, answer.getFile().getInputStream(),answer.getFile().getSize(),answer.getFile().getContentType()); // Delete the already existing file if(existingfileId!=null) fileService.deleteFile(existingfileId); if(newAnswer) answer.persist(); else answer.flush(); return answer.serializeToJson(); }
Thank you! |
|