【发布时间】:2021-12-23 17:36:55
【问题描述】:
我正在尝试上传 JSON 文件,但 MVC 控制器始终将其解释为 null。
查看:
<h3>OR</h3><br>
@Html.TextBox("jsonFile", null, new { type = "file" })
<div class="col-md-offset-2 col-md-10 ">
<input type="submit" value="Create" class="btn btn-default submit-button" formaction="Create" />
</div>
控制器:
public ActionResult Create(HttpPostedFileBase jsonFile)
{
MessageBox.Show("Create");
String str;
if (ModelState.IsValid)
{
if (jsonFile != null)
{
MessageBox.Show("File Upload Success");
StreamReader jsonReader = new StreamReader(jsonFile.InputStream);
str = jsonReader.ReadLine();
MessageBox.Show(str);
}
else
{
MessageBox.Show("Null");
}
return RedirectToAction("Index");
}
return View(projectDetail);
}
它是更大程序的一部分,我使用以下代码作为表单:
@using (Html.BeginForm( new { htmlAttributes = new { enctype = "multipart/form-data" } } ))
{
}
我得到这个按钮来上传文件,文件上传也很好,因为我可以在点击提交之前看到它的上传状态。不知道为什么它在控制器中总是为空。
【问题讨论】:
标签: c# asp.net-mvc file upload