File management for profile pictures - c#

I'm bulding an ASP.NET website where the users can upload their profile picture. I store these images under the App_Data folder and the file paths in the Users table.
So, the question is how should I update the user's profile picture? I mean, what happens if I overwrite the previous file and at the same time a request arrives to get the picture. Should I have some concurrency management?
My best solution is generating a file name for the new image(the user record in the database would be updated with this new path as well) and store the old path somewhere else, in order to delete it later.
Is this a good idea?

What I generally do in my case.
Creates a folder in my project with path ~/uploads/images. Here images is my concerned folder which is under uploads and uploads is in root.
While saving image path to db i use simple
var dbpath=
string.Format("~/Uploads/Images/{0}",Guid.NewGuid().ToString().Replace("-",string.Empty));
And from file upload control Fu.SaveAs(Server.MapPath(dbPath));
If you want to display image over the page from database, either you can use
<asp:Image runat="server" id="immm"></asp:Image> , from code behind imm.ImageUrl = dt.Rows[0]["ImagePath"].ToString"
2.If its inside grid/repeater, use simply <asp:Image runat="server" id="immm" ImageUrl='<%#Eval("ImagePath")%'></asp:Image>

No need to generate new file names. There's a very smart solution.
Alter your Users table and add a Uploadversion field. And overwrite the same file with uploaded file and just increase a upload version field in your Users table.
When your client requests the file, send the path with this version. Client may add this version as fake querystring to src path of your img element.
<img src="/[image_route]?[version]" />
For ex:
<img src="/images/john_doe/photo?16" />

Related

How to restrict json files that are stored under wwwroot folder to view from Web Browser

I have a "client-styles.css" in my wwwroot / client_folder folder. When I run my project in localhost and give that css file path in the browser its showing the entire css file in the web browser.
( localhost:5000/client_folder/client-styles.css ).
In the same way I have a folder also contains json files which contains sensitive information and those are also showing if we give the path to those Json files ( localhost:5000/client_folder/client-secrets.json ) in the browser. Is there any way restricting some files to view from web browser.
Thanks
Based on your last comment above:
It's perfectly acceptable to store css and javascript files in wwwroot. However, do not store anything secret there. Storing secrets like connectionstrings are best in EnvironmentVariables.

Accessing images without logging in

I have an ASP .Net site which requires users to log in i.e. http://www.example.com.
They login and upload images which are stored under the images folder, under the root of the site i.e.
MySite
App_Code
App_Data
Images
Employee Images
Employee1.jpg
Employee2.jpg
Employee3.jpg
Country Logos
Country1.jpg
Country2.jpg
Country3.jpg
....
I now have a second project completely separate from the above. I need to display the images from the above location but if i use http://www.example.com/images/ i am forced to log in.
How could i access the images without logging in?
You need to write controller function in order to access files in the protected directory. Then control access to that controller.

How to give URL for a file stored in app_data folder in asp.net

I have a zip file stored in app_data folder .I want to give the path ofthat file as URL for a hyperlink.How to assign it
Server.Mappath(~/App_Data/Test.zip) will give the physical location as D:/Projects/Mnv/App_Data/Test.zip , but i want to give it as URL like http://..../test.zip, so user can download
You should not use App_Data available to the users.
This is a special folder used to store internal data only (XML, sdf, etc..).
By default, this folder is not even available via Http.
Yo can create an Action in your Asp.Net MVC and use it to retun some file from this folder. A direct download, however, it is not recommended.

Force the browser to save downloaded file in a specific location

My project is an Asp.Net MVC4 web application.
Currently it has a method to generate a text file and send it to the client's browser for download.
I need to modify it to force the browser to save the file in a custom (pre-defined) location on the client's computer.
This will not be possible as it would introduce a severe security problem. A user has to decide where the file will be saved.
You can only specify a location on a server to which you have access to.
If its an internal site, then you could setup the server to save the file to a network location and return that path to the user..
If you want to show a save as, add this to your ActionResult to indicate a file download:
Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });
return myFileStreamResult
I needed to download and sort files into a rigidly defined directory structure on the client machine with no possibility of user mistakes. Ideally it would be completely automatic. I couldn't make it fully automatic, but in Chrome in Windows, I eliminated the possibility of typing mistakes with:
<a class="aDownload" href="file.txt" download="CTRL+V for suggested path/file">Download</a>
<textarea id="textareaClipboard"></textarea>
Using jQuery to listen for a click of the link, I call a function to generate the desired path and final file name, put it in the textarea, and transfer this to the clipboard using
jQuery('#textareaClipboard').select();
document.execCommand('copy' ,null ,null);
The Save As dialog pops up with "CTRL+V for suggested path/file" in the file name field. Follow the suggestion to paste the generated file name into this field, and hit Enter.
It requires a minimal amount of user action to ensure the file goes to the right directory with the right name, and the user can always reject the suggestion.
Your web application only can sending file to your client. its imposible to force download and save to spesific location, because download and save to privilege is belongs to client browser.
if user not defined default download location, it will prompt save to when download something, then if user already defined default download location. it will download automatically and save to default location.
so i think you have a little misconception with your web logic :D

resource sharing in asp.net mvc

I have 2 asp.net mvc2 projects in a solution. One is normal site for visitors use and the other one is admin back-end which is going to be separated by sub-domains like test.com and admin.test.com. The scenario is like admin will add a new item(e.g product) with image and test.com will use that image to display product. Both application are sharing one db. so there is no problem to get the item details that is coming from the db. but for item image that has been uploaded in admin directory(admin.test.com) - any idea how to get it from general domain(test.com) to display??
Also what is the best way of separating the resources like image files or even css or js files across sites and how to access them?
p.s.I'm using shared hosting.
Thanks!
You can upload to a third sub-domain or to the front end domain.
You upload to a physical folder. This folder can be an appSetting value or so.
So, you get similar settings to those in web.config appSettings:
<add key="ProductImagesPhysicalFolder" value="x:\websites\frontend\product-images" />
<add key="ProductImagesFolderUrl" value="http://frontend.com/product-images" />
Upload to the physical folder (given you setup permission in ISS to allow write access to that folder), and have a helper method like GetProductImageUrl(string imageFilename) to get the URL of the image given its filename (saved in DB).

Categories