简体   繁体   English

“ OpenFileDialog”类不包含“ ShowDialog()”和“ FileName”的定义

[英]“OpenFileDialog” class does not contain definitions for “ShowDialog()” and “FileName”

Code 在此处输入图片说明

Error List 错误清单

在此处输入图片说明

IDE: Visual Studio 2015 IDE: Visual Studio 2015
.NET Framework Version: 4.5.1 .NET Framework版本: 4.5.1
Project Template: ASP.NET MVC 项目模板: ASP.NET MVC


Notes: 笔记:

  • I already added the reference "System.Windows.Forms" to use OpenFileDialog class 我已经添加了引用“ System.Windows.Forms”以使用OpenFileDialog类
  • I added "using System.Windows.Forms" (Btw, is this necessary if I already referenced the namespace?) 我添加了“使用System.Windows.Forms”(顺便说一句,如果我已经引用了命名空间,这是否有必要?)
  • I cleaned and rebuilt the solution a few times 我清洗并重新构建了几次解决方案
  • I even closed and reopened the whole project 我什至关闭并重新打开了整个项目

Since you're using ASP.NET, you cannot use the OpenFileDialog class. 由于使用的是ASP.NET,因此无法使用OpenFileDialog类。 It is for Windows Forms applications. 它适用于Windows Forms应用程序。

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. 这是 MSDN使用FileUpload控件的一个示例

Simple example using HTML input: 使用HTML输入的简单示例:

<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. 编辑:我没有意识到这是一个MVC项目,而不是Web窗体。

You won't be able to use the asp:FileUpload control since you're not using webforms. 由于您没有使用网络表单,因此将无法使用asp:FileUpload控件。 However, it isn't hard to do it in MVC. 但是,在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>                  
}  

You cant use OpenFileDialog, because MVC doesnt allow it, what you have to do is use 您不能使用OpenFileDialog,因为MVC不允许使用,所以您必须使用

<input type="file"/>

On the front end 在前端

Edit: Just to be a bit more clear, think that you re trying to run a OpenFileDialog command on a computer which is a client, in web in general you cant use this kind of approach 编辑:只是更清楚一点,以为您尝试在作为客户端的计算机上运行OpenFileDialog命令,通常在Web中,您不能使用这种方法

here its more explained OpenFileDialog in cshtml 这里它在cshtml中的更多解释的OpenFileDialog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM