简体   繁体   English

如何允许用户选择文件夹?

[英]How to allow user to select a folder?

I want to save the path of a folder similar to what Google Chrome does: 我想保存类似于Google Chrome浏览器的文件夹的路径:

在此处输入图片说明

I want the user to be able to select a folder and then this path will be saved to the database. 我希望用户能够选择一个文件夹,然后将此路径保存到数据库。 I am just doing this on my local machine. 我只是在本地计算机上执行此操作。 I have found plenty of examples of how to save an image to a folder but I need to get the folder's path. 我找到了很多有关如何将图像保存到文件夹的示例,但我需要获取文件夹的路径。 Basically like the image, I need to create a button that will allow the user to select a folder from their machine. 基本上像图像一样,我需要创建一个按钮,该按钮将允许用户从其计算机中选择文件夹。 I need to save this folder path. 我需要保存此文件夹路径。 The problem is how to I actually select the folder path and display it in the textbox? 问题是如何实际选择文件夹路径并将其显示在文本框中?

This will help you to get started I guess; 我想这将帮助您入门。

<div class="row">
    <label for="files" class="col-md-2">Select files : </label><input type="file" multiple class="control-label col-md-10" name="files">
</div>

You will be able to improve the layout by adding an input field, more labels etc. If you need even more, you can use jquery to improve further. 您将可以通过添加输入字段,更多标签等来改善布局。如果需要更多内容,可以使用jquery进行进一步改进。

For security reasons you cannot access the folder in which a file that the user uploads to your application is stored in his client computer. 出于安全原因,您无法访问用户上传到您的应用程序的文件存储在其客户端计算机中的文件夹。 So what you probably need is to store this uploaded file in some folder on your webserver and then store this folder in your database. 因此,您可能需要将该上传的文件存储在Web服务器上的某个文件夹中,然后将该文件夹存储在数据库中。

In order to get the physical location of some folder on your server you could use the MapPath method with a relative location: 为了获取服务器上某个文件夹的物理位置,可以使用具有相对位置的MapPath方法:

 
 
 
 
  
  
  string physicalLocation = Server.MapPath("~/App_Data/");
 
 
  

and then combine this with the filename: 然后将其与文件名结合起来:

 
 
 
 
  
  
  string filename = Path.Combine(physicalLocation, "someFile.png");
 
 
  

Now you can store the filename location in your database. 现在,您可以将文件名位置存储在数据库中。


UPDATE: 更新:

Unfortunately there's no such standard HTML control that will allow you to select a folder on the client machine. 不幸的是,没有这样的标准HTML控件可让您在客户端计算机上选择文件夹。 The closest you could get is a standard ASP.NET textbox where the user will simply type this folder: 您可以得到的最接近的是标准ASP.NET文本框,用户只需在其中键入以下文件夹即可:

 <asp:TextBox runat="server" ID="SomeFolder" /> 

Now on the server side you could access the contents of this textbox and store in your database: 现在,在服务器端,您可以访问此文本框的内容并将其存储在数据库中:

 protected void SomeButtonClick(object sender, EventArgs e) { string folder = SomeFolder.Text; // store the folder that the user typed in your database } 

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

相关问题 如何允许应用程序用户选择可以保存应用程序文档的文件夹位置 - How to allow app user to select a folder location where in app documents can be saved 如何允许用户 select 在 MonoTouch 中播放视频? - How to allow user to select a video in MonoTouch? 如何允许用户在画布上选择/突出显示多个孩子? - How do I allow a user to select/highlight multiple children on a canvas? 如何允许用户在数据绑定 ComboBox 中使用 select 空白项 - How to allow user to select blank item in data bound ComboBox c#如何让用户选择文件夹然后输入 - c# How to have user select folder and then enter 带有复选框列的DataGrid允许用户选择行? - DataGrid with a checkbox column to allow the user to select rows? 允许用户选择要使用的代码的实现 - Allow user to select implementation of code to use 不允许用户选择现有日期 - dont allow user to select existing date 如何确保用户选择特定文件夹并从所选文件夹中的文件中删除文件扩展名? - How to make sure user select specific folder and remove file extension from files in the selected folder? 如何限制OpenFileDialog仅允许用户选择PDF文件? - How can I limit an OpenFileDialog to only allow the user to select PDF files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM