文件上传问题MVC - 程序集参考
我正在尝试构建一个页面,用户可以通过该页面上传文件,并将其发送到数据库。
我正在遵循教程,到目前为止我的控制器方法如下所示:
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>
但是,我目前遇到两个编译错误:
- “System.Web.HttpPostedFileBase”不包含以下定义 “HasFile”并且没有扩展方法 “HasFile”接受第一个参数 类型的 “System.Web.HttpPostedFileBase”可以 被发现(你是否缺少一个使用 指令还是程序集引用?)
- 名称“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:
- '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?)- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我找到了教程正在跟踪?
如果是这样 - 检查作者为 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.