C# 如何在 ASP.NET 中显示 OpenFileDialog 类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12914473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:57:19  来源:igfitidea点击:

How can I show up OpenFileDialog class in ASP.NET?

c#asp.netopenfiledialog

提问by snvngrc

I want to show up an OpenFileDialog box in my ASP .NET web application. I am writing in c#. I want to create a user account with an image so I need to select image from my computer and save it into a database. How can I implement it.

我想在我的 ASP .NET web 应用程序中显示一个 OpenFileDialog 框。我正在用 C# 编写。我想创建一个带有图像的用户帐户,因此我需要从我的计算机中选择图像并将其保存到数据库中。我该如何实施。

采纳答案by Tigran

You cando that using

可以使用

   <input type="file"  id="fileLoader" name="files" title="Load File" ...

The usual trick is to make it invisible, and on clicking some visibleartifact (styled link, image, button... whatever) simulatea click on fileLoader:

惯用的伎俩是使无形的,并且在一些点击可见伪像(styled linkimagebutton...等等)模拟在点击fileLoader

 $("#fileLoader").click();

回答by John Saunders

You cannot. OpenFileDialogis something for desktop applications.

你不能。OpenFileDialog是桌面应用程序的东西。

回答by Davide Piras

you cannot use that Windows Forms class in ASP.NET, you should use the FileUpload class/control.

您不能在 ASP.NET 中使用该 Windows 窗体类,您应该使用FileUpload 类/控件

Or see other alternatives: Uploading Files in ASP.net without using the FileUpload server control

或查看其他替代方案:在 ASP.net 中上传文件而不使用 FileUpload 服务器控件

回答by GWR

Almost the same as Tigran's above but I found I needed to change the JavaScript slightly to use getElementById("...") to identify the button to click(). If I just did this in HTML/CSS Tigran's code worked fine but when I used it within an .aspx file I required the change.

与上面的 Tigran 几乎相同,但我发现我需要稍微更改 JavaScript 以使用 getElementById("...") 来识别 click() 按钮。如果我只是在 HTML/CSS Tigran 的代码中执行此操作,则工作正常,但是当我在 .aspx 文件中使用它时,我需要进行更改。

.aspx

.aspx

<input type="file" id="fileLoader" name="files" title="Load File" />   
<asp:Button ID="LoginButton" runat="server"  Text="ASP click Me" onclientclick="openfileDialog()"  />

JavaScript

JavaScript

function openfileDialog() {
         document.getElementById("fileLoader").click();
     }

css

css

#fileLoader{
display:none;}