user3256868
user3256868

Reputation:

Uploaded image doesn't show in image control in asp.net

When I uploaded image stored in folder but not shows in image control. How to solve this? I am using file upload and image control within Update Panel.

private void StartUpLoad()
{

    string imgName = fuimage.FileName;

    int imgSize = fuimage.PostedFile.ContentLength;

    string ext = System.IO.Path.GetExtension(this.fuimage.PostedFile.FileName);

    if (fuimage.PostedFile != null && fuimage.PostedFile.FileName != "")
    {

        if (imgSize > 2097151)
        {
            lblimgname.Text = "alert('File is too big.')";
        }

        if (ext.ToUpper().Trim() != ".JPG" && ext.ToUpper() != ".PNG" && ext.ToUpper() != ".JPEG")
        {
            lblimgname.Text = "alert('Please choose only .jpg, .png and .jpeg image types!')";
        }
        else
        {

            string fileName = fuimage.FileName.ToString();

            string uploadFolderPath = "~/staff_image/";

            string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);

            fuimage.SaveAs(filePath + "\\" + fileName);

            imgstaff.ImageUrl = "~/Event_Image/" + fuimage.FileName.ToString();

            lblimgname.Text = fuimage.FileName.ToString();

            Response.Write("Image Saved Successfully!");

        }

    }

}

Upvotes: 0

Views: 1634

Answers (1)

Sidhdharajsinh Sodha
Sidhdharajsinh Sodha

Reputation: 163

ok please check this two lines :

string uploadFolderPath = "~/staff_image/";

and this :

     imgstaff.ImageUrl = "~/Event_Image/" + fuimage.FileName.ToString();

So basically the image uploading at the folder "staff_image" but later you are showing/displaying it from "Event_Image" folder so the uploaded image is not existed at the location "Event_Image" !

So make this change at the line :

   imgstaff.ImageUrl = "~/staff_image/" + fuimage.FileName.ToString();

I have tested it, now its working fine & displaying image properly from the location.

Upvotes: 2

Related Questions