我试图把在一起的一页,从而用户可以上载一个文件,它向一个数据库。

我是下一个教程,和我的控制方法迄今为止是这样的:

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. '系统。网。HttpPostedFileBase'并不含有定义 'HasFile'并没有扩展的方法 'HasFile'接受第一个参数 的类型 '系统。网。HttpPostedFileBase'可能 被发现了(你是缺少一个使用的 指令,或一个大会参考?)
  2. 的名称,"路径"不存在在目前情况下

这里也是一个例子什么我使用的命名空间在控制器:

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

我会非常感激如果任何人都可以点我在正确的方向,以修复这个错误。

有帮助吗?

解决方案

我想我找到了 教程 你是下列方面?

如果是这样的-检查的一部分,提交人写了一个定义扩展的方法HasFile()方法。这不是框架的一部分,因此将需要创建的,还。

第二个问题是,路径是系统的一部分。IO名字空间,所以,你需要添加这一点。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top