我是mvc的新手,我遇到了一個問題。我搜索了一個答案,我找不到一個答案,但我確信有東西跳過了我。問題是,我不知道如何在將文件上載到App_Data文件夾後訪問文件。我用我所有的論壇上發現了同樣的代碼:如何在將文件上傳到App_Data之後查看文件/使用Razor在MVC 3中上傳文件?
對於我的看法我用這個
@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成爲我的文件的鏈接。我希望我已經說清楚了。任何幫助真的會被讚賞。 感謝
我想我的數據庫只具有文件路徑,而不是文件本身。 – Robert
您能否定義「查看文件」的含義,是否要將其顯示爲鏈接,還是要顯示文件的實際內容? –