<body> <div> <!-- 上传单个文件---> <form action="/Home/UpdateFile2" enctype="multipart/form-data" method="post" id="form1"> <input type="file" name="file" id="file1" /><!--只能选择单个文件---> <input type="button" id="but" value="xxxxxxx" /> </form> <!-- 上传多个文件--> <form action="/Home/UpdateFile1" enctype="multipart/form-data" method="post" id="form2"> <input type="file" name="img" multiple="multiple" id="file2" /> <!--可选择多个文件--> </form> </div> <script> $("#but").click(function () { var files = document.getElementById("file1").files; var files0 = document.getElementById("file1").files[0]; console.log(files); console.log("test"); console.log(files0); var form_data = new FormData(); // 获取文件 var file_data = $("#file1").prop("files")[0]; console.log(file_data); // 把所以表单信息 form_data.append("id", "001"); form_data.append("name", "test"); form_data.append("img", file_data); $.ajax({ url: "/Home/UpdateFile1", data: form_data, type: "POST", async: false, contentType: false, processData: false, success: function (msg) { console.log(msg); }, error: function (e) { } }); }) </script> </body>
后台代码
var ss = Request["img"]; // form_data.append("img", file_data); 获取不到 var s4s = Request.Form["img"]; //获取不到值 var s0s = Request["form_data"]; // form_data.append("img", file_data); 获取不到 var s3s = Request.Form["id"]; //能获取到值 form_data.append("id", "001"); //获取文件 var s1s = Request.Files; //能获取到值 var s2s = Request.Files["img"]; //能获取到值