Since you're using ASP.NET, you cannot use the OpenFileDialog
class. It is for Windows Forms applications.
You'll need to use a File Upload input on your web page to upload the file. Here is one example of that from the MSDN using the FileUpload control.
Simple example using HTML input:
<input type="file" name="file" />
You'll have to update your code behind file as well.
EDIT:
I didn't realize this was for an MVC project, not web forms.
You won't be able to use the asp:FileUpload control since you're not using webforms. However, it isn't hard to do it in MVC. Refer to this article for a comprehensive example. I've extracted some of the article below.
You'll have some kind of action to render the page and accept the posted file on your controller:
[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();
}
}
And on your view, you'll have a form to upload and submit the file:
@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>
}