Webform应用程序中的OpenFileDialog

时间:2016-03-17 06:31:36

标签: asp.net vb.net webforms

我尝试在我的Webform应用程序中使用OpenFileDialog()控件,但它无效。

我试过这个编码,但它给了我错误: System.Windows.Forms.OpenFileDialog 未定义

Private Sub FnOpenFileDialog()
    Dim openfile As New System.Windows.Forms.OpenFileDialog
    openfile.Filter = String.Format("Image file (*.jpg)|*.jpg")
    openfile.Multiselect = True
    openfile.ShowDialog()
End Sub

Private Sub btnUpload_PreRender(sender As Object, e As EventArgs) Handles btnUpload.PreRender
    Dim objThread As New Thread(AddressOf FnOpenFileDialog)
    objThread.IsBackground = True
    objThread.SetApartmentState(ApartmentState.STA)
    objThread.Start()
End Sub

或者有什么方法可以做到这一点?我不想使用FileUpload控件,因为它显示文本框+按钮。我只想显示Button()控件。

我指的是这个网站How to Apply OpenFileDialog Function to a WebApplication

1 个答案:

答案 0 :(得分:1)

在ASP.NET Web应用程序中,VB代码在 Web服务器上运行。因此,即使您成功使用OpenFileDialog会导致对话框显示在服务器上,而不是浏览器中。

JavaScript也无法帮助您,因为运行JavaScripts的浏览沙箱不允许访问文件系统(从安全角度来看这很好)。您只需接受需要使用文件上载控件。您可以通过css应用样式来自定义外观。

相关问题