使用uploadify自动提交用户文件,在我的控制器方法Request.Files["Name"]中,不断返回null,但request.form不为空,当我设置断点并调试它时,我可以在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
设置对其进行自定义。