MVC 5 文件上传

MVC 5 file upload

有人可以帮我解决这个问题吗?我一直在关注如何使用 MVC 5 使用文件上传的教程,但文件一直无法上传。

我的 App_Data 文件夹中有一个上传文件夹,文件应该保存到该文件夹​​中。在我的控制器中我有这个:

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”项目,“从头开始》,如下:

  1. 创建项目:

    MSVS > 新项目 > 名称= SimpleUpload > MVC= Y

  2. 添加控制器:

    控制器 > 添加 > 添加控制器 > MVC 5 控制器 - 空 > 名称=上传控制器

    public const string UPLOADS_FOLDER = @"c:\temp";
    public ActionResult Index() { ... }
    [HttpGet] public ActionResult UploadFile() { ... }
    [HttpPost] public ActionResult UploadFile(HttpPostedFileBase file) { ... }
    
  3. 添加视图:

    视图 > 添加 > 添加视图 > 名称= 上传文件,模板= 空,使用布局页面= Y

  4. Windows 探索者:

    确保“上传文件夹”存在

  5. 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>
 }