file-upload - MVC 5 文件上传
问题描述
有人可以帮我吗?我一直在关注如何使用 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();
}
}
我添加了视图,但是当我再次尝试时,我遇到了同样的问题。
解决方案
您尝试这样做应该相当简单,而且我认为您拥有所需的所有信息。对不起,如果不是这样:(
如果您的问题没有得到解决,我鼓励您“从头开始”开始一个新的“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资源管理器:
确保“上传文件夹”存在
微博:
启动应用程序,浏览到 http://localhost:58021/Upload/UploadFile
控制器\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();
}
}
}
}
视图\上传\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>
}
推荐阅读
- weblogic - Weblogic 10.3.6 在 OutOfMemoryError 上生成空堆转储
- php - 我是否错误地使用了oop?
- python - 自定义异常处理如何工作,Python?
- android - 是否可以同时运行 Activity 和 Fragment?
- java - 等待设备时出错:AVD Nexus_5_API_25 的模拟器进程被杀死
- symfony - Symfony EasyAdminBundle v1.17.14:如何不在表单的子类中显示属性
- ansible - 为什么 Ansible 将列表转换为字符串?
- javascript - 带有 KmlLayer 的 Google 地图 API
- javascript - 比较来自 API 的两个响应但出现错误
- reactjs - 使用打字稿时方法反应getDerivedStateFromProps问题