I would like to call an image to a web page that has been uploaded by the user but unsure how to pull a specific image from a local folder by name. Below is the code used to upload the file. As the file is uploaded, I insert the file name and userid into
a table. I was thinking that I could call an image by matching the userid and name in the table to the name of the photo in the folder, but I am unsure how to do so.
Any help that can be provided would be very helpful. Thanks!
You use a WHERE clause in your SQL to filter to just rows that have the current user's ID in them:
@{
var db = Database.Open("Mumba_Data");
var sql = "SELECT * FROM Files WHERE UserId = @0";
var image = db.QuerySingle(sql, WebSecurity.CurrentUserId);
}
<img src="~/images/ProfileImages/@image.Filename" alt="" />
kookoomoo
Member
54 Points
92 Posts
Calling images uploaded by user
Oct 06, 2012 02:37 AM|LINK
I would like to call an image to a web page that has been uploaded by the user but unsure how to pull a specific image from a local folder by name. Below is the code used to upload the file. As the file is uploaded, I insert the file name and userid into a table. I was thinking that I could call an image by matching the userid and name in the table to the name of the photo in the folder, but I am unsure how to do so.
Any help that can be provided would be very helpful. Thanks!
@{ Layout = "~/_SiteLayout.cshtml"; Page.Title = "Add Profile Image"; } @{ int UserId = WebSecurity.CurrentUserId; WebImage photo = null; var Filename = ""; var imagePath = ""; var db = Database.Open("Mumba_Data"); if(IsPost){ photo = WebImage.GetImageFromRequest(); if(photo != null){ Filename = UserId + "_" + Path.GetFileName(photo.FileName); imagePath = @"images\ProfileImages\" + Filename; photo.Save(@"~\" + imagePath); var insertCommand = "INSERT INTO Files (Filename, UserId) Values(@0, @1)"; db.Execute(insertCommand, Filename, UserId); } } } <!DOCTYPE html> <html> <head> <title>Upload Profile Image</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <fieldset> <legend> Upload Image </legend> <label for="Image">Image</label> <input type="file" name="Image" /> <br/> <input type="submit" value="Upload" /> </fieldset> </form> <h1>Profile Image</h1> @if(imagePath != ""){ <div class="result"> <img src="@imagePath" alt="image" /> </div> } </body> </html>Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: Calling images uploaded by user
Oct 06, 2012 08:17 AM|LINK
You use a WHERE clause in your SQL to filter to just rows that have the current user's ID in them:
@{ var db = Database.Open("Mumba_Data"); var sql = "SELECT * FROM Files WHERE UserId = @0"; var image = db.QuerySingle(sql, WebSecurity.CurrentUserId); } <img src="~/images/ProfileImages/@image.Filename" alt="" />Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
kookoomoo
Member
54 Points
92 Posts
Re: Calling images uploaded by user
Oct 07, 2012 01:32 AM|LINK
Thank you, that worked perfectly.
Is there an IF statement I could add that would allow me to call a static image if the profile picture is null?
Mikesdotnett...
All-Star
154955 Points
19872 Posts
Moderator
MVP
Re: Calling images uploaded by user
Oct 07, 2012 06:26 AM|LINK
@{ var db = Database.Open("Mumba_Data"); var sql = "SELECT * FROM Files WHERE UserId = @0"; var image = db.QuerySingle(sql, WebSecurity.CurrentUserId); } @if(image == null){ <img src="~/images/ProfileImages/static.gif" alt="" /> } else { <img src="~/images/ProfileImages/@image.Filename" alt="" /> }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter