上传文件始终为空
Upload file is always null
当我尝试上传文件时,它始终为空,但用户名已通过。
<div class="container">
@using(Html.BeginForm("Upload", "ADImage", FormMethod.Post, new { enctype = "mulipart/form-data" }))
{
<fieldset>
<legend>AD Image Upload</legend>
<label>AD Username</label>
<div class="input-control text" data-role="input-control">
<input type="text" name="username" placeholder="Username" />
<button class="btn-clear" tabindex="-1" type="button"></button>
</div>
<label>Choose File</label>
<div class="input-control file info-state" data-role="input-control">
<input type="file" name="file" tabindex="-1" style="z-index: 0;" />
<input type="text" id="input_file_wrapper" readonly style="z-index: 1; cursor: default;" />
<button class="btn-file" type="button"></button>
</div>
<button type="submit" value="Submit">Submit</button>
</fieldset>
}
</div>
控制器
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file, string username)
{
if (string.IsNullOrWhiteSpace(username))
{
ViewBag.IsUserValid = false;
}
else if (file == null | file.ContentLength.Equals(0))
{
ViewBag.IsFileValid = false;
}
else
{
byte[] fileBytes = ReadFile(file.InputStream);
}
return RedirectToAction("Index");
}
您在表单的 enctype
字段中有错字。是
"mulipart/form-data"
但应该是
"multipart/form-data"
注意缺少的 "t"。
当我尝试上传文件时,它始终为空,但用户名已通过。
<div class="container">
@using(Html.BeginForm("Upload", "ADImage", FormMethod.Post, new { enctype = "mulipart/form-data" }))
{
<fieldset>
<legend>AD Image Upload</legend>
<label>AD Username</label>
<div class="input-control text" data-role="input-control">
<input type="text" name="username" placeholder="Username" />
<button class="btn-clear" tabindex="-1" type="button"></button>
</div>
<label>Choose File</label>
<div class="input-control file info-state" data-role="input-control">
<input type="file" name="file" tabindex="-1" style="z-index: 0;" />
<input type="text" id="input_file_wrapper" readonly style="z-index: 1; cursor: default;" />
<button class="btn-file" type="button"></button>
</div>
<button type="submit" value="Submit">Submit</button>
</fieldset>
}
</div>
控制器
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file, string username)
{
if (string.IsNullOrWhiteSpace(username))
{
ViewBag.IsUserValid = false;
}
else if (file == null | file.ContentLength.Equals(0))
{
ViewBag.IsFileValid = false;
}
else
{
byte[] fileBytes = ReadFile(file.InputStream);
}
return RedirectToAction("Index");
}
您在表单的 enctype
字段中有错字。是
"mulipart/form-data"
但应该是
"multipart/form-data"
注意缺少的 "t"。