Click here to Skip to main content
15,745,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends, I have a problem in showing images located at local drive in aspx page, how can i do this. when i give path of root folder "photos/", it works but when i give path "D:/photos/" it not showing images, my code is:

void showimages()
{

mscon = new OleDbConnection();
mscon.ConnectionString = Session["conn"].ToString();
mscon.Open();

OleDbDataAdapter msadapt = new OleDbDataAdapter("select top 10 * from stock order by id desc", mscon);
DataTable msdata = new DataTable("STOCK");
msadapt.Fill(msdata);

string imgfront,imgback,id;
labelview.Text += " ID Date Design Style Colour Quantity Stock Place Remarks ";
labelimages.Text += " ID Front Back ";

for (int i = 0; i < msdata.Rows.Count; i++)
{
imgfront = msdata.Rows[i].ItemArray[7].ToString();
imgback = msdata.Rows[i].ItemArray[8].ToString();
id = msdata.Rows[i].ItemArray[0].ToString();

string date;
DateTime olddate = Convert.ToDateTime(msdata.Rows[i].ItemArray[1].ToString());
string newdate = olddate.ToString("dd/MM/yyyy");
date = newdate;

labelview.Text += "";
labelview.Text += "" + msdata.Rows[i].ItemArray[0].ToString() + "";
labelview.Text += "" + newdate + "";
// labelview.Text += "" + msdata.Rows[i].ItemArray[1].ToString() + "";
labelview.Text += "" + msdata.Rows[i].ItemArray[2].ToString() + "";
labelview.Text += "" + msdata.Rows[i].ItemArray[3].ToString() + "";
labelview.Text += "" + msdata.Rows[i].ItemArray[4].ToString() + "";
labelview.Text += "" + msdata.Rows[i].ItemArray[5].ToString() + "";
labelview.Text += "" + msdata.Rows[i].ItemArray[6].ToString() + "";
labelview.Text += "" + msdata.Rows[i].ItemArray[9].ToString() + "";
labelview.Text += "";
labelimages.Text += "";
labelimages.Text += "" + msdata.Rows[i].ItemArray[0].ToString() + "";
labelimages.Text += " <img src='Photos/" + imgfront + "' width='400' height='400' /> ";
labelimages.Text += " <img src='Photos/" + imgback + "' width='400' height='400' /> ";
labelimages.Text += " labelimages.Text += "";

}
labelimages.Text += "
Click to view All Stock ";
Posted
Comments
Afzaal Ahmad Zeeshan 2-Feb-15 15:07pm    
Maybe because the images are present inside your Photos folder, and inside the D:Photos they're not available. Is that the case?

When you specify the path as photos/your-image.jpg, the browser resolves that path relative to the current page. For example, if the current page is http://www.yoursite.com/stock.aspx, the image path will be resolved as http://www.yoursite.com/photos/your-image.jpg - a resource on your server.

If you specify the path as D:\photos\your-image.jpg, that's a local path. The browser will either try to load the image from that path on the client's computer, where it probably doesn't exist; or, more likely, it will block the attempt to access a local resource from an internet site.

Every image, script file, stylesheet, or other external resource you include in a web page must be loaded from an internet address. You cannot use a local path.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Feb-15 16:35pm    
That's right, but you might need to explain more: the server-side code may need the absolute path on the server's host file system. (Voted 4.)
Please see Solution 3.
—SA
There more than likely is an exception being thrown in the background.

It will have something to do with the fact that the account that the website is running under from iis doesn't have access to the folder in question as it's outside of the website folders

From memory(as I don't have access to my pic) I belive you have to make sure that aspnet account has access
 
Share this answer
 
Comments
Richard Deeming 2-Feb-15 16:03pm    
I think it's more likely that the browser is refusing to attempt to load an image from the local path D:\photos\... in an HTML page that wasn't loaded from a local path.
Simon_Whale 3-Feb-15 4:02am    
As per SA, as far as I am aware all modern browsers use sand-boxing which dont allow a website to interact with the local drives of a client machine
In addition to Solution 2.

Chances are, you cannot access "D:/photo" directory even on the server's host file system.
ASP.NET applications, as all other Web applications, are normally executed in the sand-boxed environment which wont's allow you to access any local directory, unless it is under the rood directory set up for the site. And even if this local directory was under the rood directory, you should not access it by the absolute name. You need to use relative URLs or the relative path names (or relative to the root) using MapPath which will translate the names into the absolute path names needed for your code:
https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath%28v=vs.110%29.aspx[^].

—SA
 
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