views:

178

answers:

4

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?

+5  A: 

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.

Haacked
Hand-coding will not be practical since new users will be added along the way. I am looking at SkyDrive as a perfect example of what I am trying to accomplish.
MarlonRibunal
I think what he meant by hand-coding was creating the folder programatically based on the username, and was simply stating there is no existing convention in .net for this.
Quintin Robinson
Exactly. You'll need to create the folder programmatically.
Haacked
The folders will be Pre-existing...
MarlonRibunal
A: 

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.

Quintin Robinson
I guess the common counter-argument here is that "files belong to a filesystem, not a database", but it might turn out easier storing files in a DB.
Jon Limjap
That's my main reason for not doing this, but on a contextual basis it could make sense.
Quintin Robinson
A: 

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!

Jon Limjap
"...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."That is the "how?" part of my question. Any samples of this? Nice to see you here bro!
MarlonRibunal
I think @Guy gave a nice answer towards that end :)
Jon Limjap
i'll look into that...tnx!
MarlonRibunal
+3  A: 

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.

Guy