明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

ajax 提交文件

Posted on 2022-12-18 14:52  且行且思  阅读(64)  评论(0)    收藏  举报

html页面:

<form id = "uploadForm" method = "post" enctype = "multipart/form-data">
    <label>选择文件</label>
    <input id = "file" type = "file" name = "file">
    <button id = "upload" type = "button" name = "button">提交</button>
</form>

js代码:

function upload(){
    var formData = new FormData;
    var file = document.getElementById("file").files[0];
    formData.append("file",file);
    $.ajax({
        type: 'post',
        url: getRootPath() + '/xxxx/xxxxx',
        data: formData,
        dataType: "json",
        cache: false,
        processData: false,
        contentType: false,
        success: function(data){
            var result = data.returnCode;
            var msg = data.returnMessage;
            if(result === "00"){
            toastr.success("")
            }
            else{
                toastr.waring(msg);
            }
        }
})
}

 

后端代码:

@RequestMapping(value="/xxxx",method=RequestMethod.POST,produces = MediaTYpe.XXX)
@ResponseBody
public void a(@RequestParam("file") Multipartile file){
    
}