简体   繁体   English

如何在Spring MVC中将文件上传的ajax调用发送到控制器

[英]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. 在单击上载按钮时,我正在调用一个Java脚本,我想在其中编写ajax调用以将请求发送到控制器。 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. 请让我知道是否有人使用ajax调用完成了此任务。 As if i am not using ajax call i am able to upload a file and save it to local. 好像我不使用ajax调用一样,我可以上传文件并将其保存到本地。 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. 就个人而言,我从未能够执行ajax调用来更新文件。 Specially in older IE version. 特别是在较旧的IE版本中。 I use this plugin to perform the ajaxsubmit of the form wich have the File in it. 我使用此插件来执行ajaxsubmit形式的文件,其中包含文件。 Easy to use. 易于使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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