Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is image upload code.problem is on page_load where i have to give extension ".jpg" to show the picture.but it only shows .jpg picture but i want to show picture with all the extension.
If i remove .jpg is shows nothing..
please help me to show picture with all extension




C#
protected void Page_Load(object sender, EventArgs e)
       {
           //Session.Timeout = 90;

           string username = (string)(Session["UserAuthentication"]);
               if(Session["UserAuthentication"]!=null)
               {
                   Label2.Text = username;

                   Image1.ImageUrl = "Images/" + username + ".jpg";


               }


C#
private void StartUpLoad()
       {
           //get the file name of the posted image
              string username = (string)(Session["UserAuthentication"]);

           //get the size in bytes that

           int imgSize = FileUpload1.PostedFile.ContentLength;

           //validates the posted file before saving
           if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
           {
               // 10240 KB means 10MB, You can change the value based on your requirement
               if (FileUpload1.PostedFile.ContentLength > 1024000)
               {
                   Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);

               }
               else
               {
                   //then save it to the Folder
                //string[] splitter = FileUpload1.PostedFile.FileName.Split('.');
                   string ext=System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
                   string filePath = "Images/" + username+ext; //+"." + splitter[1].ToString();
                FileUpload1.SaveAs(Server.MapPath(filePath));
                Image1.ImageUrl = "~/" + filePath;
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);

               }

           }


        }


C#
protected void Button2_Click1(object sender, EventArgs e)
       {
           
           StartUpLoad();
          
       }
Posted
Updated 12-Jun-13 19:21pm
v3
Comments
[no name] 13-Jun-13 1:22am    
Please elaborate more on this
fak_farrukh 13-Jun-13 1:25am    
Image1.ImageUrl = "Images/" + username + ".jpg";
in this i have added ".jpg" to show picture but it will only show pictures with extension .jpg but i want pictures of .png and .gif extension..
Sergey Alexandrovich Kryukov 13-Jun-13 1:26am    
"All" is way to many, do you understand that? I'm afraid, you don't understand something very basic. Strictly speaking, .jpg is not "extension" (which was actually so in old obsolete file systems), it does not mean anything, well, almost. The file type is defined by its content. With .NET, counting only bitmap-based formats, you can use BMP, TIFF, PNG, JPG, ICO (did I miss anything?). Isn't it enough for your purpose?
—SA
fak_farrukh 13-Jun-13 1:31am    
Image1.ImageUrl = "Images/" + username;
but if i give only this then its shows nothing on the page_upload it upload image but on page_load shows nothing

try to store imagename with extension
eg. summer.jpg,winter.gif
and
then assiged to image url and don't metion any extension here
 
Share this answer
 
Comments
fak_farrukh 13-Jun-13 1:33am    
name of the image is store on session name
give any example to do this
yourfriendaks 13-Jun-13 2:06am    
FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));
I am totally agree with Sergery. This is "Not a good practice". In case of that, you can store the files in folder and store the url in database. While displaying, you can take image path from database and assign it to Image URL. See example:
how to save images into folder and images path in database and display images from folder in gridview based on images path in database using asp.net[^]

see your temporary solution:
If username is unique, the images will store with unique name. All you'll have to do is, search the file with username and assign the path as URL of image. Try this:
C#
protected void Page_Load(object sender, EventArgs e)
{
    string username = (string)(Session["UserAuthentication"]);
    if(Session["UserAuthentication"]!=null)
    {
       Label2.Text = username;
       //Get all the images stored in images directory
       string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
       //Search the image with same name as username
       string path = filePaths.AsEnumerable().Where(s => s.Contains(username)).First();
       //Assign the path
       Image1.ImageUrl = path;
    }
}



--Amit
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900