Grossik
Grossik

Reputation: 55

Spring ajax send value but null

I have problem with my ajax posting. Ajax:

    var formData = new FormData();
    formData.append('file', files);
    formData.append("url", url);
    $.ajax({
        url : "/servisDetail/uploadSoubor",
        type : 'GET',
        processData: false,
        contentType: false,
        data : formData,           
        success : function(response) { 
            console.log(response);
            //vypisPrilohy(response);
        },
        error: function (xhr) { }
    });

And java:

@RequestMapping(value = "/servisDetail/uploadSoubor", method= RequestMethod.GET)
public @ResponseBody
ModelMap servisDetailUploadFile(@RequestParam(value = "file",required = false) MultipartFile soubor,
                                @RequestParam(value = "url",required = false) String odkaz,
                                Locale locale){
    ModelAndView model = new ModelAndView();
    System.err.println("File: " + soubor + " and " + odkaz);
    return model.getModelMap();
}  

But print to console is: File: null and null.

Without processData: false I have ajax error: Illegal invocation and type get or post is still same

Does anyone know how to fix it?

Upvotes: 0

Views: 514

Answers (2)

Grossik
Grossik

Reputation: 55

Finally, I did it differently. Thank you.

        $.ajax({
        url : "/servisDetail/uploadSoubor",
        type : 'GET',
        dataType: 'json',
        contentType: 'application/json',
        data : {
            fileSize: files.size,
            fileName: files.name,
            url: url
        },         
        success : function(response) { 
            console.log(response);
            vypisPrilohy(response);
        },
        error: function (xhr) { }
    });  

And using ByteArrayInputStream byteArray = new ByteArrayInputStream(new byte[fileSize]);

Upvotes: 0

Anonymity
Anonymity

Reputation: 61

It is because you didn't append right file to formData. Please refer to How to use FormData for ajax file upload. I tried below code and it passed test without any issue.

<html>
<head>
<title>Test</title>
</head>
<body>
<input type="file" name="file">
<script src="jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){
    $('[name=file]').on('change', function(){
        var formData = new FormData();
        formData.append('file', $(this)[0].files[0]);
        formData.append("url", 'https://stackoverflow.com/questions/51235726/spring-ajax-send-value-but-null');
        $.ajax({
            url : "/servisDetail/uploadSoubor",
            type : 'POST',
            processData: false,
            contentType: false,
            data : formData,           
            success : function(response) { 
                console.log(response);
                //vypisPrilohy(response);
            },
            error: function (xhr) { }
        });
    })
});
</script>
</body>
</html>

I got output like File: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@4b5db24c and https://stackoverflow.com/questions/51235726/spring-ajax-send-value-but-null in the console

Upvotes: 1

Related Questions