gpt4 book ai didi

java - 如果附件不可用,ajaxform 不会提交值

转载 作者:行者123 更新时间:2023-12-01 02:19:52 27 4
gpt4 key购买 nike

我是一名 Java 初学者,打算创建一个 Web 应用程序。我正在使用 ajaxform 向 Controller 提交一份表单。元素很少,其中之一是 type=file

我的 Controller 方法如下

    @RequestMapping(value = { "/addReplies" }, method = {
org.springframework.web.bind.annotation.RequestMethod.POST,
org.springframework.web.bind.annotation.RequestMethod.GET } )
@ResponseBody
public Map<String,Object> addReplies(
@Valid @ModelAttribute("Replies") Replies replies,
BindingResult result, HttpServletRequest request,
HttpServletResponse response,Locale locale,Principal principal,
@RequestParam(value = "fileData2",required=false) CommonsMultipartFile fileData2[]) throws ServletException,
IOException {
//perform some opraton
}

如果数据中有可用附件,则一切正常工作,否则不会进入此方法。

如果我从方法中删除 @RequestParam(value = "fileData2",required=false) CommonsMultipartFile fileData2[] 此参数,则可以正常工作,但通过这种方式我无法获取附件。

请尝试理解我的问题并尽快给我所有可能的解决方案。

如果我不使用ajax并使用regul提交表单,此方法效果很好

最佳答案

我也这样做但我的编码也可以工作

执行此解决方案

@RequestMapping(value="/saveUserTask", method=RequestMethod.POST)
@ResponseBody
public Map<String,String> saveUserTask(
@RequestParam( value = "title" , required = false) String title,
@RequestParam( value = "projectid" , required = false) int projectid,
@RequestParam( value = "mileid" , required = false,defaultValue ="-1") int mileid,
@RequestParam( value = "responsible" , required = false) int responsible[],
@RequestParam( value = "startDate" , required = false) Date startDate,
@RequestParam( value = "endDate" , required = false) Date endDate,
@RequestParam( value = "description" , required = false) String description,
@RequestParam( value = "priority" , required = false, defaultValue="1") int priority,
@RequestParam( value = "workHours" , required = false) int workHours,
@RequestParam(value = "attachment" , required = false) MultipartFile myFile,
HttpServletRequest request,Principal principal,RedirectAttributes redirectAttributes,Locale locale) throws ParseException, IOException{
String fileAttachPath;
if(myFile!=null)
fileAttachPath=request.getSession().getServletContext().getRealPath("/WEB-INF/attechments/"+ new Date().getTime() + "_" + myFile.getOriginalFilename());
else
fileAttachPath="";
}

关于java - 如果附件不可用,ajaxform 不会提交值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25994125/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com