Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (FileUpload1.HasFile)
{
    string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    //string path = Server.MapPath(@"~\\"+Session["parentfolder"].ToString() +"\\"+ Session["brandname"].ToString() + "\\" + Seasonfolders.SelectedItem.Text + "\\" + stylefolders.SelectedItem.Text + "\\Images\\" + FileName);
    string root = Server.MapPath("~");
    string path = Path.GetDirectoryName(root);
    string path1 = Path.GetDirectoryName(path);
    string rootfolder = Path.GetDirectoryName(path1);
    string imagepath = rootfolder + Session["brandname"].ToString() + "\\" + Seasonfolders.SelectedItem.Text + "\\" + stylefolders.SelectedItem.Text + "\\Images\\" + FileName;
    FileUpload1.SaveAs(imagepath);
    //objBAL.SaveImage("Image", Session["brandname"].ToString(), Seasonfolders.SelectedItem.Text, stylefolders.SelectedItem.Text, FileName, imagepath, Convert.ToInt32(Session["Empcode"]));
    uploadedimage.ImageUrl = Server.MapPath(@"~/"+imagepath);
    uploadedimage.DataBind();

}

uploadedimage is ID for Image control. The path of imagepath is E:\Folder1\Folder2\Folder3\Images\1.png

The image is getting saved but I cannot able to display the uploaded image. Do I need to add anything in this line which is to display an image ..

C#
uploadedimage.ImageUrl = Server.MapPath(@"~/"+imagepath);
uploadedimage.DataBind();
Posted
Updated 15-May-14 20:30pm
v2
Comments
DamithSL 16-May-14 2:43am    
what is the value of root?
Sriram Ramachandran 16-May-14 2:45am    
E:\Sriram\Project\sgportalapplication

1 solution

try below


C#
if (FileUpload1.HasFile)
 {
   string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
   FileUpload1.SaveAs(Server.MapPath("~/" +Session["brandname"].ToString() +
                        "/" + Seasonfolders.SelectedItem.Text +"/" + stylefolders.SelectedItem.Text
                        "/" + Images +"/") + FileName );

   Image1.ImageUrl = "/" +Session["brandname"].ToString() +
                        "/" + Seasonfolders.SelectedItem.Text +"/" + stylefolders.SelectedItem.Text
                        "/" + Images +"/" + FileName ;
 }


Based on your comments, you are storing images outside the root folder. there are few ways to handle this situation.
check Displaying images that are stored outside the Website Root Folder[^]
Or Make D:\YourUploadFolder\ a virtual directory in the website using IIS management - then you could send URL to set the Image1.ImageUrl
 
Share this answer
 
v3
Comments
Sriram Ramachandran 16-May-14 3:01am    
Could not find a part of the path 'E:\Sriram\Project\sgportalapplication\Monoprix\SS_15\STYLE_3\Images\Chrysanthemum.jpg'. ...
'E:\Sriram\Project\sgportalapplication (where application resides)
My folder access requirement is E:\Monoprix(Session) and so on

Actually I need to store the image in E:\Monoprix(session[brandname])\Season(season.sel.item)\style(style.item)\Images\filename..
The image should store in corresponding folders using session["brandname"]... and should also retrieve the image from this location only ....
DamithSL 16-May-14 3:06am    
you are saving to folder not inside your web root, ImageUrl need relative path ( relative to your web site root folder) or full web URL. in your case you can't give both of them
Sriram Ramachandran 16-May-14 3:05am    
should access all files using rootfolder(E:\\)
DamithSL 16-May-14 3:09am    
is there any reason you can't make upload folder inside your web site root?
Sriram Ramachandran 16-May-14 3:19am    
Actually after completing the coding part, I will move all folders in C:\\inetpub\\wwwroot(IIS)Application and required folders to access... so the root would give me C:\ path where I can find the images in specific ...
Upload folder is in respective folders where I'll move to server(IIS) after completing the code...

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