简体   繁体   中英

how to send ajax call of file upload to controller in spring MVC

Hi i have a dialog box on that i am uploading a file. And on click of upload button i am calling a java script in which i want to write a ajax call to send the request to the controller. and also what should be the controller code to save the file into local machine and return success string. Pls let me know if any one has done this using ajax call. As if i am not using ajax call i am able to upload a file and save it to local. thanks in advance.

 I want to submit a file but not through form action submit but ajax call something like this var formdata=document.getElementById("Uploadfile"); 
var fd = new FormData(formdata);
fd.append("CustomField","This is an additional data");
$.ajax({
    type: "POST",
    url: contextPath+"/fileUploadForm",
    data: fd,
    enctype:"multipart/form-data",
    processData: false,
    contentType: false, 

    success: function(data){
            alert("inside success ***");
    } ,
    error: function(){
        alert("error has occured");

        }
});

And also what would be the controller code ??

Personnally, i never been able to perform an ajax call to update a file. Specially in older IE version. I use this plugin to perform the ajaxsubmit of the form wich have the File in it. Easy to use.

http://jquery.malsup.com/form/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM