繁体   English   中英

Request.Files [“”]继续返回null

[英]Request.Files[“”] Keep returning null

使用uploadify自动提交用户文件,在我的控制器方法Request.Files [“Name”]保持返回null但request.form不为null,我可以在request.form中看到设置断点并调试它时的文件。 我错过了什么吗? 我在mvc2上测试这个,但我计划在mvc4上使用它。

<link href="../../Content/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="../../Scripts/jquery.uploadify.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $('#file_upload').uploadify({
            'swf': '/Content/uploadify.swf',
            'uploader': '/Home/UploadFile',
            'auto': true


            // Your options here
        });
    });
</script>
 </head>
   <body>           
   <%--<% using (Html.BeginForm("UploadFile", "Home", FormMethod.Post,
 new { enctype = "multipart/form-data" }))
 { %>--%>
  <input type="file" name="file_upload" id="file_upload" style="margin-bottom: 0px" />

 <%-- <% } %>--%>

控制器方法:

    [HttpPost]
    public ActionResult UploadFile(HttpPostedFileBase file)
    {           
        var theFile = Request.Files["file_upload"];
        return Json("Success", JsonRequestBehavior.AllowGet);
    }

如果我添加一个提交按钮,然后提交它,它将工作。 我需要自动但没有提交按钮。

IIRC Uploadify使用fileData作为参数。 所以:

var theFile = Request.Files["fileData"];

甚至更好:

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase fileData)
{          
    // The fileData parameter should normally contain the uploaded file
    // so you don't need to be looking for it in Request.Files
    return Json("Success", JsonRequestBehavior.AllowGet);
}

当然,如果您对此名称不满意,可以使用fileObjName设置自定义它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM