Click here to Skip to main content
15,886,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,i am using asp.net image url control
I want to display image according to employee id.
Everything is working fine but image is just displaying as Cross sign(X) in Image url wen I select id.please give me suggestion

What I have tried:

string imgpath = Server.MapPath("~/image/" + FileUpload1.FileName);
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(uploadFolder + "Test" + DropDownList1.SelectedItem.Text + extension);
Image1.ImageUrl = "~/" + imgpath;
ImageButton1.ImageUrl = "~/" + imgpath;
Labelupload.Text = "File uploaded successfully as: " + "Test" + extension;
Posted
Updated 22-Aug-16 0:34am

From you code I can see that the saved file name and path will be different to the image1.ImageUrl path. This is because between the lines
C#
string imgpath = Server.MapPath("~/image/" + FileUpload1.FileName);

and
C#
Image1.ImageUrl = "~/" + imgpath;

you are doing this:
C#
uploadFolder + "Test" + DropDownList1...

So the paths are different.

Also, this line is hardcoded so change the code to show the properly constructed path:
C#
Labelupload.Text = "File uploaded successfully as: " + "Test" + extension;

change to:
C#
string saveTo = uploadFolder + "Test" + DropDownList1.SelectedItem.Text + extension;
Labelupload.Text = "File uploaded successfully as: " + saveTo;
 
Share this answer
 
C#
string imgpath = Server.MapPath("~/image/" + FileUpload1.FileName);


This gives the location of the file on the server's disc, so it will be something like "c:\inetpub\wwwroot\yoursite\images\image.jpg"

That isn't a valid image source so you can't set it as the ImageUrl property of the image, it would result in an image url of

"/images/c:\inetpub...."

which is obviously not going to work on the client.

You also save the file to a completely different location and filename anyway.

Your code also has security problems, it allows me to upload .aspx files, or exe files and they'll be saved to your server.

Anyway, read some articles on uploading and showing images and you'll find code that does the kind of thing you're looking to do, none of your code is correct.
 
Share this answer
 

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