问题描述
有人可以在这里帮助我吗?我一直在关注如何使用MVC 5使用文件上传的教程,但是文件始终无法上传。
我的App_Data文件夹中有一个Uploads文件夹,应将文件保存到其中。在我的控制器中,我有这个:
using System.IO;
namespace [project_name].Controllers
public class [controller_name]: Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
ViewBag.Message = "";
try
{
// Verify that the user selected a file
if (file.ContentLength > 0)
{
// extract only the filename
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"),fileName);
file.SaveAs(path);
}
// redirect back to the index action to show the form once again
ViewBag.Message = "File Upload Successful";
return RedirectToAction("Index");
}
catch(Exception ex)
{
ViewBag.Message = ex.Message;
return View();
}
}
}
在我看来,我有这个:
@{
ViewBag.Title = "File Upload";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="col-md-4">
@using (Html.BeginForm("Index","[controller_name]",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
@ViewBag.Message<br />
@Html.TextBox("file","",new { type = "file" })
<input type="submit" value="Upload" />
}
</div>
</div>
<br />
那么,我想念什么?该项目仍在开发中,我正在使用Visual Studio 2017,因此仍在使用localhost
进行调试。当我使用断点时,它显示file
仍然是null
。我从catch块得到的错误是“对象引用未设置为对象的实例。”。
有人对我在这里做错了什么有任何想法吗?
更新: 我尝试将操作名称更改为此:
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult UploadFile() { return View(); }
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
ViewBag.Message = "";
try
{
// Verify that the user selected a file
if (file.ContentLength > 0)
{
// extract only the filename
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"),fileName);
file.SaveAs(path);
}
// redirect back to the index action to show the form once again
ViewBag.Message = "File Upload Successful";
return RedirectToAction("Index");
}
catch(Exception ex)
{
ViewBag.Message = ex.Message;
return View();
}
}
我添加了视图,但是当我再次尝试时,我遇到了同样的问题。
解决方法
您要这样做的应该非常简单,我认为,您已拥有所需的所有信息。抱歉,不是这种情况:(
如果您的问题未解决,建议您从头开始创建一个 new “ hello world”项目,如下所示:
-
创建项目:
MSVS>新项目> 名称= SimpleUpload> MVC = Y
-
添加控制器:
控制器>添加>添加控制器> MVC 5控制器-空> 名称= UploadController
public const string UPLOADS_FOLDER = @"c:\temp"; public ActionResult Index() { ... } [HttpGet] public ActionResult UploadFile() { ... } [HttpPost] public ActionResult UploadFile(HttpPostedFileBase file) { ... }
-
添加视图:
视图>添加>添加视图> 名称= UploadFile,模板=空,使用布局页面= Y
-
Windows资源管理器:
确保“上传文件夹”存在
-
MSVS:
启动应用,浏览至http:// localhost:58021 / Upload / UploadFile
Controllers \ UploadController.cs
using System.IO;
using System.Web;
using System.Web.Mvc;
namespace SimpleUpload.Controllers
{
public class UploadController : Controller
{
public const string UPLOADS_FOLDER = @"c:\temp";
// GET: Upload
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult UploadFile()
{
return View();
}
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
try
{
if (file.ContentLength > 0)
{
string fileName = Path.GetFileName(file.FileName);
string path = Path.Combine(UPLOADS_FOLDER,fileName);
file.SaveAs(path);
}
ViewBag.Message = "File Uploaded Successfully!!";
return View();
}
catch
{
ViewBag.Message = "File upload failed!!";
return View();
}
}
}
}
Views \ Upload \ UploadFile.cshtml
@{
ViewBag.Title = "UploadFile";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm("UploadFile","Upload",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
<div>
@Html.TextBox("file","",new { type = "file" }) <br />
<input type="submit" value="Upload" />
@ViewBag.Message
</div>
}