如何将 OpenFileDialog 函数应用到 Web 应用程序

发布于 2024-10-21 07:03:31 字数 245 浏览 6 评论 0 原文

我正在开发一个Web应用程序,其中一个页面有一些必要性,用户应该选择系统中的任何文件夹,例如如果他选择ex:D:\xyz,那么这个路径应该保存在数据库中,我已经编写了代码用于存储到数据库并保留所有内容。但实际上我认为,如果它就像一个浏览按钮单击功能来选择文件夹,我所做的是,因为我不知道如何在 Web 应用程序中制作它,我只是使用一个文本框来让用户能够输入路径。有人可以帮助我如何在网页中应用“打开文件对话框”功能吗?

预先感谢,

问候 卡延。

I am devoloping a Web Application, in which one Page has some neccessity that User should select any folder in the System and for ex if he selects ex:D:\xyz, then this Path should be Saved in Database, I have written the Code for Storing in to Database and Remaining Every thing. But actually i think that if it is like a Browse Button click function to select the Folder, what i have made is, as i dont know how to make it in Web Application simply i used a textbox to be able to user to type the Path. Can, any one help me how to apply the Open File Dialog Functionality in a Web Page?

Thanks in Advance,

Regards
Kalyan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

左秋 2024-10-28 07:03:31

没有 ASP.Net 控件来选择文件夹。

作为一个简单的解决方法,您可以使用 FileUpload - 控件让用户选择一个文件,您可以 保存该文件的文件夹。但这仅适用于 IE,因为其他浏览器不会发布完整的文件路径,而只会发布文件名(安全限制)。

编辑:正如 Ben 提到的,这在 IE>7 中也不起作用:
http:// /blogs.msdn.com/b/ie/archive/2008/07/02/ie8-security-part-v-compressive-protection.aspx文件上传控制部分)

There is no ASP.Net Control to select folders.

As a simple workaround you could use the FileUpload-Control to let the user select a file and you can save the folder of that file. But that would work only in IE because other browsers don't post the full file-path but only the file-name(security restrictions).

Edit: As Ben mentioned that doesn't work in IE>7 either:
http://blogs.msdn.com/b/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx (section File Upload Control)

手心的温暖 2024-10-28 07:03:31

对于网络应用程序:

System.IO.FileInfo file = new System.IO.FileInfo(file1);
Response.AddHeader("Content-Disposition", "attachment;filename=\"" +"Filename"+".ext"+ "\"");
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = file.Extension.ToLower();
            Response.WriteFile(file.FullName);
            Response.End();

For Web Application:

System.IO.FileInfo file = new System.IO.FileInfo(file1);
Response.AddHeader("Content-Disposition", "attachment;filename=\"" +"Filename"+".ext"+ "\"");
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = file.Extension.ToLower();
            Response.WriteFile(file.FullName);
            Response.End();
清风夜微凉 2024-10-28 07:03:31

通过右键单击解决方案浏览项目名称添加引用 system.windows.forms,将 Windows 打开文件对话框引用添加到 Web 应用程序
然后按照这里的编码风格,我已经给出了 VB 示例代码,如果您遇到任何问题告诉我,您可以将其转换为 C#

VB代码

   Import system.threading

   Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)  Handles btnUpload.Click

    Dim objThread As New Thread(AddressOf FnOpenFileDialog)
    objThread.IsBackground = True
    objThread.SetApartmentState(ApartmentState.STA)
    objThread.Start()
End Sub

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

Add windows openfile dialog reference toweb application by righ click on solution explore project name add reference system.windows.forms
then follow this coding style here i have given VB sample code you can convert this to C# if your facing any problem tell me.

VB Code

   Import system.threading

   Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)  Handles btnUpload.Click

    Dim objThread As New Thread(AddressOf FnOpenFileDialog)
    objThread.IsBackground = True
    objThread.SetApartmentState(ApartmentState.STA)
    objThread.Start()
End Sub

Private Sub FnOpenFileDialog()
    Dim openfile As New System.Windows.Forms.OpenFileDialog
    'openfile.InitializeLifetimeService()
    'openfile.Filter = String.Format("Image file (*.jpg)|*.jpg|All files (*.*)|*.*")
    openfile.Filter = String.Format("Image file (*.jpg)|*.jpg")
    openfile.Multiselect = True
    openfile.ShowDialog()
End Sub
零度° 2024-10-28 07:03:31
  • 用户选择的是文件还是文件夹?
  • 该位置是在服务器上还是在他的本地计算机上?
  • 您是否正在尝试上传某些内容,或下载某些内容,或者只是想要路径?

如果您想要服务器文件或文件夹的路径,则必须让服务器提供可供选择的可能文件夹的列表。

如果您只需要本地文件的路径,则无法从 Web 应用程序执行此操作。

最简单的解决方案是告诉用户在资源管理器中浏览并复制路径,以粘贴到对话框中。

  • Is the user selecting a file or a folder?
  • Is the location on the server, or on his local computer?
  • Are you trying to upload something, or download something, or just want the path?

If you want the path of a server file or folder, you have to get the server to give a list of possible folders to choose from.

If you just want the path of a local file, you cannot do this from a web application.

The easiest solution is to tell the user to browse in Explorer and copy the path, to paste in your dialog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文