由于您使用的是 ASP.NET,因此您不能使用 OpenFileDialog 类。它适用于 Windows 窗体应用程序。
您需要在网页上使用文件上传输入来上传文件。 Here is one example 来自 MSDN 使用 FileUpload 控件。
使用 HTML 输入的简单示例:
<input type="file" name="file" />
您还必须更新您的代码隐藏文件。
编辑:
我没有意识到这是针对 MVC 项目,而不是 Web 表单。
您将无法使用 asp:FileUpload 控件,因为您没有使用网络表单。但是,在 MVC 中做到这一点并不难。 Refer to this article 是一个全面的例子。我摘录了下面的一些文章。
您将有某种操作来呈现页面并在您的控制器上接受发布的文件:
[HttpGet]
public ActionResult UploadFile()
{
return View();
}
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
try
{
if (file.ContentLength > 0)
{
string _FileName = Path.GetFileName(file.FileName);
string _path = Path.Combine(Server.MapPath("~/UploadedFiles"), _FileName);
file.SaveAs(_path);
}
ViewBag.Message = "File Uploaded Successfully!!";
return View();
}
catch
{
ViewBag.Message = "File upload failed!!";
return View();
}
}
在您看来,您将有一个表单来上传和提交文件:
@using(Html.BeginForm("UploadFile","Upload", FormMethod.Post, new { enctype="multipart/form-data"}))
{
<div>
@Html.TextBox("file", "", new { type= "file"}) <br />
<input type="submit" value="Upload" />
@ViewBag.Message
</div>
}