1,前端页面:外面还有一个Form (PrmTest/Report) <form class="cmxform form-horizontal tasi-form" id="submitForm" method="POST" action="@Request.Url.PathAndQuery" novalidate="novalidate">
 
@using (Html.BeginForm("UploadFile", "PrmTest", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {
          <label class="control-label col-lg-1">请选择</label>
          <div class="col-lg-11">
               <input id="file-zh" name="file-zh[]" type="file" multiple class="file-loading" />
           </div>
   }</form>

2,后端控制器方法:
 [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            var fileName = file.FileName;
            var filePath = Server.MapPath(string.Format("~/{0}", "ProjectFile"));
            file.SaveAs(Path.Combine(filePath, fileName));
            return View(Content(fileName));
        } [HttpPost]
        public ActionResult Report(string file)
        {
           ……
        }3,问题现象:(1)选择文件后点上传,跟踪发现没有进入UploadFile方法,却进入Report方法;
(2)若不加form,把file控件修改成: <input id="file-zh" name="file-zh[]" type="file"data-upload-url="/PrmTest/Upload"  multiple class="file-loading" />  此时可以进入控制器UploadFile方法,但是HttpPostedFileBase file对象为空。请问如何解决??非常感谢!!!

解决方案 »

  1.   

    form套form?然后又是整体提交?你还是改成ajax提交吧,h5支持提交被本地文件流了
      

  2.   

    不支持form嵌套,那就不用;
    把嵌套的form去掉,如何把文件上传?关键文件流对象为空。
      

  3.   

    如下:
        @using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <label class="control-label col-lg-1">请选择</label>
            <div class="col-lg-11">
                <input id="file-zh" name="file" type="file" multiple class="file-loading" />
            </div>
            <button type="submit">asd</button>
        }
      

  4.   

     public ActionResult UploadFile(HttpPostedFileBase file)
            {
                var fileName = file.FileName;
                var filePath = Server.MapPath(string.Format("~/{0}", "ProjectFile"));
                file.SaveAs(Path.Combine(filePath, fileName));
                return View(Content(fileName));
            }
      

  5.   

    回复楼上:必须使用submit类型的按钮来实现上传功能?
    <button type="submit">asd</button>
      

  6.   

    使用这种方式: <input id="file-zh" name="file-zh[]" type="file"data-upload-url="/PrmTest/Upload"  multiple class="file-loading" />  如何把本地文件对象传至控制器方法?
      

  7.   


    这种方法自然是可以的
    不知道楼主为什么带着那么多的属性
    直接 <input type="file" id="fileUpLoad" name="fileUpLoad" />不就可以了吗 ,  不过 name属性是必须带的通过 ajax 向action发送异步请求 就行了
     
    var data = new FormData();
    var file = $("#fileUpLoad").prop("files")[0];
    data.append("file", file);
     $.ajax({
                url: "~",
                type: 'post',
                data: data,
                contentType: false,  //必须: 需要设置为false,不然后台拿不到数据
                processData: false,  //必须: 传送DOM信息,所以设为false
                dataType: "json",
                success: function (res) {
                },
                error: function (res) {
     
                }        });
      

  8.   

    input[type="file"]选择文件后会自动触发onchange事件,可以在onchange中执行form.submit()
      

  9.   

    回复楼上:我是在网上找到的选定文件后带有预览功能的CSS展示样式,所以看到比较多。