Reputation: 14751
I am developing a website for the project on of my course.this is a system with a lot of user and each user can upload files on server. now i have two way for implementation that:
the implementation of second way is easy but i think create a lot of folder(each user have distinct folder) may decrease the speed when every one try to load them from sever.
whats your opinion? what way is better?
Upvotes: 1
Views: 250
Reputation: 36723
Basically, save the association of File
and User
in the database using foreign keys. For example:
File
--------
FileId int
UserId (FK) int
PathToFile nvarchar
You then just save the path to the file to the table and keep the actual file in the computers filesystem.
Then in your application you can retrieve a list of files for User Foo
and serve them up easily.
The process varies between ASP.Net Webforms and ASP.Net MVC. Which are you using?
Upvotes: 0
Reputation: 1319
I don't see any major issues with choice #2. Be aware that enumerating collections of files and directories will give you better performance than arrays. Appending a guid to the file name will keep the file names unique. You can remove that guid from the filename when serving the file as a download to the user.
Upvotes: 0
Reputation: 42440
A better way would be to have a single folder called uploads
.
Everytime a user uploads a file, generate a GUID, and assign this GUID as the filename. This will ensure that filenames will never collide.
After saving the file, store the following in a database: the original filename given by the user, the GUID name of the file and userid.
You can now retrieve files uploaded by any user by first looking up the GUID from the DB and then fetching the file from the filesystem.
You can generate GUIDs using the Guid.NewGuid()
method.
Upvotes: 1