Sriram
Sriram

Reputation: 37

how to display image using imageurl in 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 ..

            uploadedimage.ImageUrl = Server.MapPath(@"~/"+imagepath);
            uploadedimage.DataBind();

Upvotes: 2

Views: 36861

Answers (3)

Aniket
Aniket

Reputation: 181

Try this

Create Data Folder in your root directory

 if (FileUpload1.HasFile)
    {
        string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);              
        string imagepath =Server.MapPath("~/Data/"+FileName);
        FileUpload1.SaveAs(imagepath); 

        uploadedimage.ImageUrl="~/"+imagepath;
    }

Upvotes: 1

deostroll
deostroll

Reputation: 11975

Hosting websites or stuff on iis, does not work this way. There are a few concepts one needs to learn on that front, but the best place to start with is understand what is virtual directory.

One quote from the this page:

In IIS 7, each application must have a virtual directory, known as the root virtual directory, and maps the application to the physical directory that contains the application's content

So it means this is a directory where the application's "content" reside; it could be simple text files, images, etc, to complex server side pages like aspx or even classic asp, or php, etc. Now anything out of this directory is not accessible by the hosted web application.

Hence the path you intend to share doesn't work that way. There are a few options to handle this scenario.

  1. In iis you could create a sub-virtual directory, and map its path to where your images are stored to the physical location where the images reside.

  2. Provided your web application (when hosted on iis) has access to the path where the images reside, you can write code to read the file, and send the stream of bytes back so that your web page can render image properly.

2nd approach is usually implemented by a handler (ashx) via which you can pass the image name as query string argument, and return the bytes. Hence in short you do something like this:

uploadedImage.ImageUrl = "~/MyImageHandler.ashx?filename=foo.png" //in ur server code.

In the handler you write something like this:

public class MyImageHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
    // Comment out these lines first:
    // context.Response.ContentType = "text/plain";
    // context.Response.Write("Hello World");

    context.Response.ContentType = "image/png";

    var filepath = @"E:\your_image_dir\" + Request.QueryString["filename"];

    //Ensure you have permissions else the below line will throw exception.
    context.Response.WriteFile(filepath);

    }

    public bool IsReusable {
    get {
        return false;
    }
    }
}

Upvotes: 2

Manish Gautam
Manish Gautam

Reputation: 516

The image Url in your image should be like this

"~/"+ imagepath

Try removing Server.MapPath

Upvotes: 1

Related Questions