0

I am trying to uplooad a file and some other fields in jsp the file gets uploaded but the request.getParameter("fieldName") is not working

My HTML Form

<form action="SurveyServlet" id="upload-form" method="post" enctype="multipart/form-data">
  Upload Image
  <input type="file" id="merge_notification" name="merge_notification"  required="required"/>
  <small>Please upload merge notification (Allowed file types are png,jpg)</small>
  Notification Date
  <input type="date" id="merge_notification_date" name="merge_notification_date" required="required"/>
  Remarks
  <textarea id="notification_remarks" name="notification_remarks"></textarea>
  <input type="hidden" id="merging_district" name="merging_district" value="sometest">
  <input type="submit" name="submit_btn" value="Merge School" class="admin-button merge-school-btn">

My Ajaxform Code

<script type="text/javascript">
    $(function () {
         $('#upload-form').ajaxForm({
            success: function(msg) {
            alert(msg);
        },
        error: function(msg) {
            $("#upload-error").text("Couldn't upload file");
        }
    });
    });

Servlet code for file upload

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
    /*Upload form*/
    if (ServletFileUpload.isMultipartContent(request)) {
        String UPLOAD_DIRECTORY = "c:/mergeSchoolNotifications";

        //Creates the directory if it does not exist in the specified location
        String dest_path = "/" + request.getParameter("merging_district");//create another directory inside
        String savePath = UPLOAD_DIRECTORY + dest_path;
        File uploadDir = new File(UPLOAD_DIRECTORY);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        File upload_sub_dir = new File(UPLOAD_DIRECTORY + dest_path);
        if (!upload_sub_dir.exists()) {
            upload_sub_dir.mkdir();
        }
        try {
            List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for (FileItem item : multiparts) {
                if (!item.isFormField()) {
                    String format = "none";
                    String name = new File(item.getName()).getName();
                    int index = name.lastIndexOf(".");
                    if (index > 0) {
                        format = name.substring(index + 1);
                        format = format.toLowerCase();
                    }
                    item.write(new File(savePath + File.separator + "MyfileName." + format));
                }
            }
            request.setAttribute("message", "uploaded");
        } catch (Exception e) {
            request.setAttribute("message", "File(s) upload failed due to " + e.getMessage() + "!");
        }
    }
    /*Upload form ends here*/
}

The path of the uploaded file is like this C:\mergeSchoolNotifications\null\MyfileName.jpg

My question is how can I dynamically create the subdirectories and then save the path of the uploaded file and some other fields in the sql db

Thanks.

  • First of all, I don't see a field with `name=fieldName`. Hence you are not getting any value on your `Servlet`. Also, do you want to send the directory_path (as text) with the file so that at `Servlet` end, you can join them together and store there subsequently? – hiren Apr 19 '18 at 06:59
  • @hiren the FieldName was just an example there is a hidden field in the form named (merging_district) for now I hardcoded it, I am getting that field in the servlet like this request.getParameter("merging_district") but it is returning null,it should get (sometest) value and should create a directory by that name – Said Yousafzai Apr 19 '18 at 07:15
  • What happens if the input is not hidden. I mean remove the `type=hidden` and insert 'sometest' in the input box and then submit. Just to see the behavior. – hiren Apr 19 '18 at 08:12
  • I changed the input type from hidden to text and still creating directory by the name of null (C:\mergeSchoolNotifications\null\MyfileName.png) – Said Yousafzai Apr 19 '18 at 08:37

0 Answers0