首页 > 其他分享 >ajax 提交文件

ajax 提交文件

时间:2022-12-18 15:00:49浏览次数:42  
标签:文件 false 提交 formData ajax file var data

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){
    
}

 

标签:文件,false,提交,formData,ajax,file,var,data
From: https://www.cnblogs.com/Fooo/p/16990387.html

相关文章