1

This is my form :

<form name="CIMtrek_Compliance_Daily_Shipments" enctype="multipart/form-data">
<input type="file" id="CIMtrek_comments" name="CIMtrek_comments" value="" />
    <button id="upload" onclick="uploadCommentFile()">Upload</button>
</form>

and this is my ajax call using jquery :

function uploadCommentFile(){
    $("#upload").live("click", function() {
        var file_data = $("#CIMtrek_comments").prop("files")[0];   // Getting the properties of file from file field
        var form_data = new FormData();                  // Creating object of FormData class
        form_data.append("file", file_data)              // Appending parameter named file with properties of file_field to form_data
        //form_data.append("user_id", 123)                 // Adding extra parameters to form_data
        $.ajax({
                    type: 'POST',
                    url: "/CIMtrek_Compliance_Daily_Shipments_FileUpload",
                    dataType: 'script',
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: {
                         uploadFile:  file_data
                       },
                    success: function (msg) {
                        global.getElementById("CIMtrek_uploadedFileName").innerHTML=msg;
                    }
           })
    })

}

and this is my spring controller :

 @RequestMapping(value = "/CIMtrek_Compliance_Daily_Shipments_FileUpload", method = RequestMethod.POST)
    public String createComments(@RequestParam("uploadFile") CommonsMultipartFile uploadItem,
            HttpServletRequest request) {
        String uploadedFileName="";
        try {
            MultipartFile file = uploadItem;
            String fileName = null;
            InputStream inputStream = null;
            OutputStream outputStream = null;
            if (file.getSize() > 0) {
                inputStream = file.getInputStream();

                System.out.println("size::" + file.getSize());
                fileName = request.getRealPath("") + "/WEB-INF/resources/Attachment"+ file.getOriginalFilename();

                System.out.println("path : "+request.getRealPath("") + "/WEB-INF/resources/Attachment");
                outputStream = new FileOutputStream(fileName);
                System.out.println("fileName:" + file.getOriginalFilename());

                int readBytes = 0;
                byte[] buffer = new byte[10000];
                while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
                    outputStream.write(buffer, 0, readBytes);
                }
                outputStream.close();
                inputStream.close();
            }
            uploadedFileName =file.getOriginalFilename();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uploadedFileName;
    }

but i get the following exception when i click on upload button :

HTTP Status 400 - 
The request sent by the client was syntactically incorrect.

what could be the problem, Please help me to identify.

Best Regards.

1
  • I am really surprised to see no body faced this kind of problem it seems and no inputs also :( Commented Jan 30, 2013 at 7:30

1 Answer 1

1

Follow this one it helped and solved my problem :

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.