【发布时间】:2011-11-10 06:18:18
【问题描述】:
我是 mvc 新手,遇到了一个问题。我到处寻找答案,但找不到答案,但我很确定有些东西跳过了我。问题是我将文件上传到 App_Data 文件夹后不知道如何访问它。我使用在所有论坛上找到的相同代码:
在我看来,我使用这个
@using (Html.BeginForm("Index", "Home", FormMethod.Post,
new { enctype="multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="submit" />
}
对于我的控制器,我使用这个
public class HomeController : Controller
{
// This action renders the form
public ActionResult Index()
{
return View();
}
// This action handles the form POST and the upload
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
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
return RedirectToAction("Index");
}
}
我的模特是
public class FileDescription
{
public int FileDescriptionId { get; set; }
public string Name { get; set; }
public string WebPath { get; set; }
public long Size { get; set; }
public DateTime DateCreated { get; set; }
}
问题是我想将文件上传到数据库,然后将 WebPath 链接到我的文件。我希望我说得够清楚。任何帮助将不胜感激。 谢谢
【问题讨论】:
-
我希望我的数据库只有文件的路径,而不是文件本身。
-
您能定义一下“查看文件”的含义吗?您想将其显示为链接,还是想显示文件的实际内容?
标签: c# asp.net-mvc-3 file-upload view razor