Reputation: 1581
I want to upload multiple files to the controller but its taking only one.
I am using multipart for file transfer. How to get the multiple files to the controller side. I am not able to access bytes and filename. Its throwing errors,
@RequestMapping(value = "/home/step38/uploadReport", method = RequestMethod.POST)
public ModelAndView uploadReport(
@RequestParam(value = "fileName") List<MultipartFile> files,
@RequestParam(value = "pLogId") String pLogId,
HttpServletRequest request, HttpSession session)
{
int applicationNameId = 0;
String fileName;
String typeOption = "Raw Particle Data";
for(MultipartFile file:files)
fileName = file.getOriginalFilename();
logger.debug("step3.1 upload particle count -- Start");
ModelAndView modelAndView = createModelAndView(ToogleStep.step38);
setCurrentStep(session, modelAndView, ToogleStep.step38);
String view = "redirect:/home/step38";
modelAndView.setViewName(view);
try
{
User user = (User) session.getAttribute(Constants.USER_DB);
Project current_project = (Project) session.getAttribute(Constants.PROJECT);
Credential credential = (Credential) session.getAttribute(Constants.AUTH_USER);
boolean checkOK = true;
if (current_project != null && SystemUtils.projectEditable(current_project, credential))
{
long projectId = current_project.getId();
if(checkOK)
{
byte[] bytes = file.getBytes();
HashMap<String,String> options= new HashMap<String,String>();
//added pLogId in the options(this will contain demoToogleFileInfoId)
options.put(ToogleReportDataConstants.TTL_PROCESS_LOG_ID_OPTION,pLogId);
String toogleFileId = reportService.uploadReport(user, projectId, fileName, typeOption, bytes, applicationNameId, options);
}
}
}
Upvotes: 1
Views: 1172
Reputation: 177
You are not looping through at the right location. try looping it after you have your modelAndView(view)
int applicationNameId = 0;
String typeOption = "Raw Particle Data";
ModelAndView modelAndView = createModelAndView(ToogleStep.step38);
setCurrentStep(session, modelAndView, ToogleStep.step38);
String view = "redirect:/home/step38";
modelAndView.setViewName(view);
// upload multiple files.
for(MultipartFile file:files){
String fileName= file.getOriginalFilename();
and then you will be able to access bytes and filename. Give this a try. Atleast by looking at your problem I can suggest and if you can give more specific error, I can help.
Upvotes: 1