4b0
4b0

Reputation: 22323

image control unable to display image

my code is:

string filename = FileUploader.PostedFile.FileName.Substring(fuImage.PostedFile.FileName.LastIndexOf("\\") + 1);
if(fuImage.HasFile)
  {
      FileUploader.SaveAs(Server.MapPath("Modules/NewUserProfile/UserPic/" + filename));
      imgUser.ImageUrl = Server.MapPath("Modules/NewUserProfile/UserPic/" + filename);
  }

imgUser is id of asp:Image Control.Image is uploaded in desire folder but its not display image in image control.What i am doing wrong here? Is there any postback issue.Thanks.

Upvotes: 1

Views: 824

Answers (5)

Banketeshvar Narayan
Banketeshvar Narayan

Reputation: 3899

To run this code please understand the following things:

  1. Server.MapPath() is used for getting physical Path like: D:/Img/Upload/.. so it is good idea to get path for saving image.
  2. But in the case when you are getting the image for binding it to image control then you must have to use virtual path instead of Physical path. Virtual path like: localhost/demo/upload/myimage.jpg.

Upvotes: 2

Neha
Neha

Reputation: 2965

newpic.ImageUrl = Page.ResolveURL("~/Pictures")+filename;

Server.MapPath returns physical drive location which is not useful when you are assigning to the imageurl.

Upvotes: 0

Kishore Kumar
Kishore Kumar

Reputation: 12884

I think you should wait for the image to completely upload and then set the Image path to the image.

Also after rendering into the Browser use FireBug in Mozilla or Chrome Inspect Elemnt to check whether the Image has been rendered properly by looking at the image. If it broken then your image path is wrong. Try to give relative path and check.

Upvotes: 0

E.J. Brennan
E.J. Brennan

Reputation: 46879

I usually debug problems like this by first doing a view-source with your browser, and making sure the URL in the HTML source is what you expect. Then try copying the url that shows up in your view-source and pasting it into the address bar of your browser, and see if it shows up.

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94653

Use relative path for ImageUrl property.

imgUser.ImageUrl = "Modules/NewUserProfile/UserPic/" + filename;

OR use root operator ~ if Modules folder is located at root of web-app.

imgUser.ImageUrl = "~/Modules/NewUserProfile/UserPic/" + filename;

Upvotes: 2

Related Questions