Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,iam uploading files to a folder called "UploadedImages",and everything is going fine,but i have a an image control which i want it to show the image but it is not doing,knowing that when i upload the image,the cancel button on the image turns into another icon.This is my code.I hope that anyone help me.

protected void Button1_Click(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            Boolean fileOK = false;
            String path = Server.MapPath("UploadedImages");
            if (FileUpload1.HasFile)
            {
                String fileExtension =
                    System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }
            if (fileOK)
            {
                try
                {
                    FileUpload1.PostedFile.SaveAs(path
                        + FileUpload1.FileName);
                    Label1.Text = "File uploaded!";
                    Image1.ImageUrl = path + FileUpload1.FileName;

                }
                catch (Exception ex)
                {
                    
                    Label1.Text = ex.Message;
                }
            }
            else
            {
                Label1.Text = "Cannot accept files of this type.";
            }
        }


    }
Posted

Hi,
Image1.ImageUrl will take relative Url like "~/MY_IMAGE.JPG" so after uploading assign relative URl to your image control, that will do the trick.

Hope this will help.
 
Share this answer
 
Comments
GenJerDan 30-Jan-11 22:52pm    
Not to hijack or anything, but I'm doing something not disimilar and wonder if this is more efficient than doing the for loop above, since the loop will run its entire course, even if the first extension hits.

string FileTypesString = ".gif.jpg.png.jpeg.";
Ext = Path.GetExtension(file.ToLower()) + ".";
if (!String.IsNullOrEmpty(Ext))
{
if (FiletypesString.Contains(Ext))
{
fileOK = true;
Monjurul Habib 1-Mar-11 14:44pm    
nice point..5+
Try this,

C#
Image1.ImageUrl = "~/UploadedImages/" + FileUpload1.FileName;
 
Share this answer
 
Comments
Monjurul Habib 1-Mar-11 14:44pm    
nice..5+
Here is one sample working code snippet. You have to provide Relative paths with ImageURL.
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       if (FileUpload1.HasFile)
       {
           string filename = Guid.NewGuid().ToString() + ".jpg";
           FileUpload1.PostedFile.SaveAs(Server.MapPath("Images\\") +filename);
           aspimage.ImageUrl = "~/Images/" + filename;
       }
   }
 
Share this answer
 
v2
Comments
Ankur\m/ 31-Jan-11 1:05am    
Edited for syntax.
Monjurul Habib 1-Mar-11 14:44pm    
nice!5+

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