文件上传问题MVC - 程序集参考

发布于 2024-09-29 06:55:54 字数 1358 浏览 1 评论 0原文

我正在尝试构建一个页面,用户可以通过该页面上传文件,并将其发送到数据库。

我正在遵循教程,到目前为止我的控制器方法如下所示:

public ActionResult Index()
{
    ViewData["Message"] = "File Upload";
    foreach (string upload in Request.Files)
    {
        if (!Request.Files[upload].HasFile()) continue;
        string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
        string filename = Path.GetFileName(Request.Files[upload].FileName);
        Request.Files[upload].SaveAs(Path.Combine(path, filename));
    }

    return View();
}

这也是我的视图的示例:

<p>
    <% using (Html.BeginForm("", "home", FormMethod.Post, new { enctype = "multipart/form-data" }))
       { %>
    <input type="file" name="FileUpload1" /><br />
    <input type="submit" name="Submit" id="Submit" value="Upload" />
    <% } %>
</p>

但是,我目前遇到两个编译错误:

  1. “System.Web.HttpPostedFileBase”不包含以下定义 “HasFile”并且没有扩展方法 “HasFile”接受第一个参数 类型的 “System.Web.HttpPostedFileBase”可以 被发现(你是否缺少一个使用 指令还是程序集引用?)
  2. 名称“Path”在当前上下文中不存在

这也是我在控制器中使用名称空间的示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;

如果有人能指出我的正确方向,我将非常感激修复此错误的方向。

I'm trying to put together a page whereby a user can upload a file, and it goes to a database.

I'm following a tutorial, and my controller method so far looks like this:

public ActionResult Index()
{
    ViewData["Message"] = "File Upload";
    foreach (string upload in Request.Files)
    {
        if (!Request.Files[upload].HasFile()) continue;
        string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
        string filename = Path.GetFileName(Request.Files[upload].FileName);
        Request.Files[upload].SaveAs(Path.Combine(path, filename));
    }

    return View();
}

Here is also an example of what my view looks like:

<p>
    <% using (Html.BeginForm("", "home", FormMethod.Post, new { enctype = "multipart/form-data" }))
       { %>
    <input type="file" name="FileUpload1" /><br />
    <input type="submit" name="Submit" id="Submit" value="Upload" />
    <% } %>
</p>

I'm currently getting two compile errors however:

  1. 'System.Web.HttpPostedFileBase' does not contain a definition for
    'HasFile' and no extension method
    'HasFile' accepting a first argument
    of type
    'System.Web.HttpPostedFileBase' could
    be found (are you missing a using
    directive or an assembly reference?)
  2. The name 'Path' does not exist in the current context

Here is also an example of what I'm using for namespaces in the controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;

I'd be very grateful if anyone could point me in the right direction to fixing this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2024-10-06 06:55:54

我想我找到了教程正在跟踪?

如果是这样 - 检查作者为 HasFile() 方法编写自定义扩展方法的部分。它不是框架的一部分,因此您还需要创建它。

第二个问题是 Path 是 System.IO 命名空间的一部分,因此您也需要添加它。

I think I found the tutorial you were following?

If so - check the part where the author has written a custom extension method for the HasFile() method. It's not part of the framework, so you would need to create that also.

The second issue is that Path is part of the System.IO namespace, so you would need to add that too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文