【发布时间】:2012-12-06 09:07:10
【问题描述】:
我想创建一个表单,用户可以在其中编辑个人资料并根据需要上传图片, 这是我的看法
//表单字符串@using(Html.BeginForm("EditProfile","Employee",FormMethod.Post,
new { enctype = "multipart/form-data " }))
//输入HTML <input type="file" name="file" id="file"/>
//提交<input type="submit" name="SubmitBtn" value="SaveProfile" />
<input type="submit" name="SubmitBtn" value="Cancel" />
现在是我的控制器
//Profile Modification
[Authorize]
[HttpPost]
public ActionResult EditProfile(employer model, string SubmitBtn,HttpFileCollection file)
{
switch (SubmitBtn)
{
case "SaveProfile":
try
{
if (ModelState.IsValid)
{
//the file is always null
if (file != null)
{
//Function I want to Apply on file
model.logoname = logouploded(file);
return RedirectToAction("Profile", "Employer");
}
return RedirectToAction("EditProfile");
}
//ChangeEmployeeProfile(model);
}
catch (Exception ex)
{
ViewBag.warn = ex.Message;
return View(model);
}
break;
case "Cancel":
return RedirectToAction("index");
}
return View(model);
}
现在无论我上传什么文件,文件总是为空 我也尝试过将 HttpFileCollectionBase 作为 Action Function 中的参数仍然文件为空
只是提到员工模型只包含图像文件名 因为我只想将图像名称保存在数据库中。
【问题讨论】:
-
您好,您在回帖后签入
Request.Files收藏了吗? -
删除 "multipart/form-data " 中的最后一个空格。正确:“multipart/form-data”
标签: c# asp.net-mvc asp.net-mvc-3