Image is not displayed

  • 9 years ago

    hi all,

    I am using the following code to upload and image and display the same in imagebox on press of a button.

    Boolean fileOK = false;

    String path = Server.MapPath("~/UploadedImages/");if (fupldPhoto.HasFile)

    {

    String fileExtension = System.IO.Path.GetExtension(fupldPhoto.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

    {

    fupldPhoto.PostedFile.SaveAs(path + txtEmpCd.Text.Trim() +
    "_Image");

    imgEmpPhoto.ImageUrl = path + txtEmpCd.Text.Trim() + "_Image"+ fileExtension;

    Page.RegisterStartupScript("Message", "<script> alert('File uploaded!')</script>");

    }

     

    HTML Code:

    <tr>

    <td align="center" colspan="2">

    <asp:Image ID="imgEmpPhoto" runat="server" Width="124px" />&nbsp;</td>

    </tr>

    <tr>

    <td colspan="2" style="height: 24px">

    <asp:FileUpload ID="fupldPhoto" runat="server" Width="170px" /><asp:Button ID="btnUpload" runat="server"

    Text="upload" OnClick="btnUpload_Click" /></td>

    </tr>

     

    I can see the image in the UploadedImages Folder.....but it is not displayed in the imagebox(imgEmpPhoto). Let me know where I am going wrong.

    Regards,

    Royal

  • 9 years ago

    Hi,

    if you execute the code and get the broken image, when you right click that image and click properties, what does it give the URL as?  Does it match what it should be?

    Also, a small point on your array iteration code, i would have used something like:

    if(array.indexOf(allowedExtensions, fileextension) > -1)

    {

     // true then item exists in the array

    }

    else

    {

    //false, it doesnt

    }

     

    i find it much easier to read in code that an interation routine

    Si

  • 9 years ago

    when I check the path of the image, it shows the correct path of the image. But the image is not visible, a small cross is displayed at the left hand top corner.

    Regards,

    Royal

  • 9 years ago

    Put a breakpoint on whatever line this is.

    imgEmpPhoto.ImageUrl = path + txtEmpCd.Text.Trim() + "_Image"+ fileExtension;

    You have to be missing part of the path or the directory that you are keeping these images in. Have you tryed to access a file in this directory?, am thinking that maybe the properties of directory are not set right.

  • 9 years ago

    Hi,

    Just spotted a possible problem:

    fupldPhoto.PostedFile.SaveAs(path + txtEmpCd.Text.Trim() + "_Image");

    imgEmpPhoto.ImageUrl = path + txtEmpCd.Text.Trim() + "_Image"+ fileExtension;

    That is your code, shouldnt it match?  i.e.

    fupldPhoto.PostedFile.SaveAs(path + txtEmpCd.Text.Trim() + "_Image"+ fileExtension);

    imgEmpPhoto.ImageUrl = path + txtEmpCd.Text.Trim() + "_Image"+ fileExtension;

    Regards

    Si

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There's no test like production” - Anon