4

我有一个 ajax 调用:发生的情况是我单击了一个图像,该图像单击了我不希望显示的“输入文件”。当输入 val() 更改时uploadListener(),调用方法,这基本上是一个将文件获取到服务器的 ajax 方法“将其上传到服务器”代码如下所示:html:

<img id="${assayAssessRequest.id}" src="[@spring.url '/images/buttons/upload.jpg'/]" onclick="uploadFile(this);" title="Upload File" />
<form id="uploadForm" action="[@spring.url '/assay/designability/uploadFile.htm'/]" method="POST" enctype="multipart/form-data">
        <div style="display: none;">
        [@spring.formInput path="multiPartBean.file" fieldType="file" attributes="title='path' class='upload' accept='.txt,.csv,.zip'" /]
        [@spring.formHiddenInput path="multiPartBean.fileName" attributes=" onchange='uploadListener();'class='uploadFileName'" /]
        [@spring.bind "multiPartBean"/]
        </div>
        <input type="submit" id="uploadButton" value="upload" />
    </form>

脚本:

function uploadFile(){
    document.getElementById('inputFile').click();
}

function uploadListener(){
   $('#uploadForm').attr("action","[@spring.url    '/assay/designability/uploadFile.htm'/]");
   alert($('#uploadForm').attr('action'));
   this.document.getElementById("uploadForm").submit = true;
   alert("After Submit");
   return false;
}

服务器控制器:

@Controller
@PreAuthorize("isAuthenticated()")
@RequestMapping("/assay/designability")
@SessionAttributes({"assayAssessmentsInitializersBean","assayAssessmentsRequestsDetailsBean"})
public class AssayDesignabilityController extends AssayBaseController {

    @RequestMapping(value = "/uploadFile",method= RequestMethod.GET)
    public String uploadFile(@RequestParam(value = "file")Object file){
        MultipartFile multipartFile=(MultipartFile)file;

        logger.info(multipartFile.getName());
        return multipartFile.getName();
    }
}

现在当我做所有这些时,响应让我胡说八道,当我尝试调试时,我从来没有进入控制器方法。有什么帮助吗??

编辑: 现在我尝试提交它我已经更新了代码并且我有相同的行为没有响应。

4

2 回答 2

2

感谢@Ian,我通过避免ajax 调用和重新实现我的代码解决了我的问题。

这是代码:

HtmlFreemarker

<form id="uploadForm" action="#" method="POST" enctype="multipart/form-data">
    <div class="instruction popup_inst">
        <span class="popup_logo">[@spring.message "pandaLogo"/]</span>

        <div class="float_right">
            <input type="button" id="cancelBtn" class="btn" onclick="refreshParentTable();closePopup();" value="[@spring.message "cancelButton"/]" />
            <input class="btn" type="submit" id="submit" onclick="validateFileInput();" value="[@spring.message "uploadButton"/]" />
        </div>

    </div>
    <span class="popup_title">[@spring.message "uploadFile"/]</span>
    <div class="popup_container">
        [@spring.bind "assayAssessmentsRequestBean"/]
        [@spring.formInput path="assayAssessmentsRequestBean.designabilityFile.file" fieldType="file" attributes="title='path' class='upload' accept='.txt,.csv,.zip'" /]
        [@spring.formHiddenInput path="assayAssessmentsRequestBean.designabilityFile.fileName" attributes="class='uploadFileName'" /]
        [@spring.formHiddenInput path="assayAssessmentsRequestBean.dateOfAssessment" attributes="" /]
        [@spring.formHiddenInput path="assayAssessmentsRequestBean.id" attributes="" /]
    </div>
    <input id="uploadfile" type="hidden" value="${uploadfile}"/>
</form>

控制器:

@RequestMapping(value = "/uploadFile",method= RequestMethod.POST)
    public ModelAndView uploadFile(@ModelAttribute(value = "assayAssessmentsRequestBean")AssayAssessmentsRequestBean assayAssessmentsRequestBean,HttpSession session) throws PanDaApplicationException {
//mycode
}

根据这次经验,我建议 evreyone 避免通过 ajax 调用发送文件。我使用弹出窗口创建表单并提交上传操作。感谢大家试图帮助

于 2013-04-04T19:05:26.980 回答
1

如果我没看错,提交动作指向“uploadfile.HTM”

$('#uploadForm').attr("action","[@spring.url '/assay/designability/uploadFile.htm'/]");

但是控制器中的方法仅映射到uploadFile(没有.htm)

@RequestMapping(value = "/uploadFile",method= RequestMethod.GET)

也许您的 web.xml / servlet-spring.xml 上有另一个配置使这成为可能,但否则,这可能是问题所在。

希望这可以帮助

问候

于 2013-04-04T15:36:54.343 回答