asp.MVC 利用HTML formData对象把整个表单+文件上传
前端HTML
<div class="x-body"> <form class="layui-form" id="ff"> <div class="layui-form-item"> <label for="username" class="layui-form-label"> <span class="x-red">*</span>文章标题 </label> <div class="layui-input-inline"> <input type="text" id="title" name="username" required="" lay-verify="required" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item layui-form-text"> <label for="desc" class="layui-form-label"> 图片上传 </label> <div class="layui-input-block"> <input type="file" name="files"/> </div> </div> <div class="layui-form-item layui-form-text"> <label for="desc" class="layui-form-label"> 文章内容 </label> <div class="layui-input-block"> <textarea placeholder="请输入内容" id="content" name="desc" class="layui-textarea" lay-verify="required"></textarea> </div> </div> <div class="layui-form-item"> <label for="L_repass" class="layui-form-label"> </label> <button class="layui-btn" lay-filter="add" lay-submit=""> 增加 </button> </div> <form /> </div>
JS代码
var formData = new FormData($("#ff")[0]); $.ajax({ url: "/Home/briefAdd", type: "POST", async: false, contentType: false, processData: false, data: formData, success: function (data) { }, error: function (data) { } });
后台代码
public ActionResult briefAdd() { var name=Request.Form["username"]; var file= Request.Files["files"]; return View(); }