用ajax上传文件的若干问题,老是报:Posted content type isn't multipart/form-data。参考网上若干文章:比如http://harttle.com/2016/07/04/jquery-file-upload.html 等,进行若干尝试,按理应该是可以上传成功的吧?是否是我又说忽视?
页面代码(有点乱,别介意哈):
<html> <head> <title>测试</title> <script type="text/javascript" src="/js/jquery-1.4.4.min.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function() { //var form = new FormData(document.getElementById("form1")); /* var formData = $(document.getElementById("form1")).serialize(); $.ajax({ url: "/test/upload", type: "post", data: formData, async: false, cache: false, contentType: false, processData: false, headers: {'Content-Type': 'multipart/form-data'}, success:function(data){ console.log("over.."); }, error:function(e){ alert("错误!!"); } }); */ /* var formData = new FormData(); formData.append('file', $('#file')[0].files[0]); */ /* $.ajax({ url: '/test/upload', type: 'POST', cache: false, data: new FormData($('#form1')[0]), processData: false, contentType: false }).done(function(res) { }).fail(function(res) {}); */ var files = $('#avatar').get(0).files; var data = new FormData(); data.append('file', files[0]); $.ajax({ url: '/test/upload', type: 'POST', data: data, cache: false, processData: false, contentType: false }); }); }); </script> </head> <body> <h1>测试测试测试</h1> <div class="table_box"> <br><br><br> <!-- <form id="form1" action="/test/upload" method="post" enctype="multipart/form-data" > --> <!-- <form id="form1" enctype="multipart/form-data"> --> <!-- <input type="file" id="file" name="file"></input> <input type="text" id="aa" name="h1"></input> <input type="text" id="bb" name="h2"></input> <input type="button" onclick="" id="btn" value="上传"> --> <!-- </form> --> <form> <input type="file" id="avatar" name="avatar" multiple> <input type="button" id="btn" value="上传"> </form> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> </div> </body> </html>
后台代码:
public void upload(){ UploadFile file = getFile("file", "\\"); System.out.println(getPara("h1")); System.out.println(getPara("h2")); System.out.println(file.getUploadPath()+""+file.getFileName()+""+file.getOriginalFileName()); renderJson(Tools.getResult(200)); }
尝试了网上N种ajax上传,然后用jfinal来处理,结果大多都是报Posted content type isn't multipart/form-data,还有一次是报 分界不明确。没有用别的框架试过,但是按道理网上那么多种方法总有一种能行的吧,结果每一种能OK?!是否是我忽视了什么鬼?求指出指出指出!!!