如何在 ASP.NET 中将用户与其各自的文件夹连接起来?
当userA上传文件时,他的文件将上传到folderA,当userB上传时,他的文件将上传到folderB,以此类推。 ASP.NET 中的角色/配置文件。 文件夹将是预先存在的。 任何人?
When userA uploads a file, his files will be uploaded to folderA, when userB, to folderB, and so on. Roles/Profiles in ASP.NET. Folders will be pre-existing. Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能需要手动编写代码。 ASP.NET 没有任何内在的东西来管理用户文件。 但是,执行此操作的代码应该相对简单。 假设用户名是唯一的并且永远不会更改,您可以将用户名与路径组合(使用 Path.Combine)并上传到该位置。 我还会锁定该位置,以便其他人无法访问它。
You'll probably want to hand-code that. There's nothing intrinsic to ASP.NET for managing user files. However, the code to do so should be relatively easy. Assuming the username is unique and never changes, you can combine the username with a path (use Path.Combine) and upload to that location. I would also lock down that location so that nobody else can access it.
我过去的做法是使用基本上传文件夹(例如 uploads),并在该文件夹中使用数据库中的用户 ID 创建一个文件夹。 因此,对于用户 ID 为 145 的用户,结构将为 ..\uploads\145。
我的代码所做的第一件事是检查该文件夹是否存在,如果不存在,则调用 Directory.Create() (或其他方法)语法是)在上传之前创建文件夹。
您可能会发现有用的更多信息:我还使用 GUID 重命名文件,这样可以避免上传 2 个同名文件时发生名称冲突。 缺点是您通常需要维护一个包含原始文件名和物理(GUID)文件名的表。
The way that I've done it in the past is to use a base upload folder (say uploads) and in that folder create a folder using the user's ID from the DB. So the structure would be ..\uploads\145 for user with a user ID of 145.
The first thing that my code does is to check to see if the folder exists and if not then calls a Directory.Create() (or whatever the syntax is) to create the folder before uploading.
Further info that you might find helpful: I also rename the file using a GUID which avoids name conflicts if they upload 2 files with the same name. The downside is that you will normally need to maintain a table with the original filename and the physical (GUID) filename.
您可以只检查该文件夹是否存在,如果不存在则为用户创建它,但这会带来安全隐患。 您可能还想尝试将数据存储在数据库中并将其与用户绑定。我猜这取决于您让用户上传的内容。
You can just check for the existance of the folder and create it for the user if it doesn't exists, but there are security implications for this. You might also want to try and store data in a database and tie it to a user.. this depends on what you are letting users upload I guess.
有几种方法可以做到这一点:
使用表单身份验证
如果您使用表单身份验证,您可以设置一个约定,其中用户的用户名或 ID 可以作为服务器中路径的基础,在该约定中,用户可以上传文件。 请注意,您的用户将无法直接访问该文件夹:用户也应该能够通过您的 Web 应用程序从您的服务器下载文件。
使用 Windows 身份验证
如果您使用 Windows(例如 ActiveDirectory)身份验证,您可以向用户提供对文件夹物理位置和通过 Web 应用程序的访问。
PS - 很高兴在这里见到你马龙!
There are a few ways you can do this:
Using Forms Authentication
If you use forms authentication, you can set a convention wherein a user's username or id can serve as the basis for a path in your server where the user can upload a file. Note that your user will not have direct access to that folder: the user should be able to download the files from your server via your web application as well.
Using Windows Authentication
If you use windows (e.g., ActiveDirectory) authentication, you can provide user access to both the physical location of the folder and via a web application.
P.S. - Glad to see you here Marlon!