我在HomeController中有这段代码
[HttpPost]
public ActionResult NewRequest(int? id, FormCollection form, HttpPostedFileBase uploadFile)
{
if (uploadFile != null && uploadFile.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(uploadFile.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/Documents/Files"), fileName);
uploadFile.SaveAs(path);
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index");
}
我的NewRequest视图就是这个
@using (Html.BeginForm("NewRequest", "Controllers", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
<input type="file" name="uploadFile" />
<input type="submit" value="Upload" />
}
问题是上传的文件,.txt文件f.x.永远是空的。
答案 0 :(得分:0)
您的代码很好。我唯一能想到的情况就是路由配置可能不对。
我已经使用此配置进行测试,它就像一个魅力。
routes.MapRoute(
name: "NewRequest",
url: "Controllers/NewRequest",
defaults: new { controller = "Controllers", action = "NewRequest" }
);