asp.net-mvc HttpPostedFileBase 在 ASP.NET MVC 中总是返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8551528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpPostedFileBase always return null in ASP.NET MVC
提问by Joshua Son
I have a problem when I upload a file in ASP.NET MVC. My code is below:
我在 ASP.NET MVC 中上传文件时遇到问题。我的代码如下:
View:
看法:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index2</h2>
@using (Html.BeginForm("FileUpload", "Board", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" />
<input type="submit" />
}
Controller:
控制器:
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile != null && uploadFile.ContentLength > 0)
{
string filePath = Path.Combine(Server.MapPath("/Temp"), Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
return View();
}
But uploadFile always returns null. Can anyone figure out why??
但是uploadFile 总是返回null。谁能弄清楚为什么?
回答by dotnetstep
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index2</h2>
@using (Html.BeginForm("FileUpload", "Board", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="uploadFile"/>
<input type="submit" />
}
you have to provide name to input type file to uploadFilein order to model binding work in ASP.net mvc and
also make sure that name of your input type file and argument nameof HttpPostedFileBaseis identical.
你必须提供姓名输入类型的文件,以uploadFile以模型绑定工作在ASP.net MVC和
也确保您输入的文件类型和参数名的名字的HttpPostedFileBase是相同的。
回答by roblem
I had tried most of the solutions posted online for this topic, but found it better to use a workaround instead..
我已经尝试了针对该主题在线发布的大多数解决方案,但发现最好使用解决方法。
It really didn't matter what I did the HttpPostedFileBase and/or HttpPostedFile were always null. Using the HttpContext.Request.Files collection seemed to work with no hassles at all.
我做了什么并不重要,HttpPostedFileBase 和/或 HttpPostedFile 总是为空。使用 HttpContext.Request.Files 集合似乎没有任何麻烦。
e.g.
例如
if (HttpContext.Request.Files.AllKeys.Any())
{
// Get the uploaded image from the Files collection
var httpPostedFile = HttpContext.Request.Files[0];
if (httpPostedFile != null)
{
// Validate the uploaded image(optional)
// Get the complete file path
var fileSavePath =(HttpContext.Server.MapPath("~/UploadedFiles") + httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf(@"\")));
// Save the uploaded file to "UploadedFiles" folder
httpPostedFile.SaveAs(fileSavePath);
}
}
In the above example I only grab the first file, but it is just a matter of looping though the collection to save all files.
在上面的例子中,我只抓取了第一个文件,但这只是一个循环通过集合来保存所有文件的问题。
HTH
HTH
Rob
抢
回答by pawel
In my scenario the problem was with id attribute, I had this:
在我的场景中,问题出在 id 属性上,我有这个:
<input type="file" name="file1" id="file1" />
The soultion was to remove id:
解决方案是删除 id:
<input type="file" name="file1" />
回答by jmoreno
While not the answer to this specific user, I would like to point out that HTML requires that the form tag has an enctype attribute with the value multipart/form-data.And of course both the attribute and it's value must be correct.
虽然不是这个特定用户的答案,但我想指出HTML 要求表单标签具有值为 multipart/form-data 的 enctype 属性。当然,属性和它的值都必须是正确的。
For mvc, this means that when using beginform, you should use the version with the htmlAttributes parameter
对于 mvc,这意味着在使用 beginform 时,应该使用带有 htmlAttributes 参数的版本
回答by sandeep talabathula
There can be another scenario also. In my case, I was getting this issue because I was directly rendering script tag in my MVC view and IE is giving issue there.
也可能有另一种情况。就我而言,我遇到了这个问题,因为我直接在我的 MVC 视图中呈现脚本标记,而 IE 在那里给出了问题。
Correct code in view should be as below:
正确的代码应该如下:
@section scripts
{
<script>
$(document).ready(function () {
$('.fileinput').fileinput();
...
}

