从aspx页面中的c#代码显示本地驱动器路径中的图像 [英] Show images from local drive path from c# code in aspx page

查看:88
本文介绍了从aspx页面中的c#代码显示本地驱动器路径中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,我在aspx页面显示位于本地驱动器的图像时遇到问题,我该怎么办呢。当我给根文件夹photos /的路径时,它有效但当我给路径D:/ photos /它没有显示图像时,我的代码是:



void showimages()

$

mscon = new OleDbConnection();

mscon.ConnectionString = Session [conn]。ToString();

mscon.Open();



OleDbDataAdapter msadapt = new OleDbDataAdapter(select top 10 *来自id desc的库存订单,mscon);

DataTable msdata = new DataTable(STOCK);

msadapt.Fill(msdata);



string imgfront,imgback,id;

labelview.Text + =ID日期设计风格颜色数量库存地方备注;

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();



字符串日期;

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 + =< img src ='image / delete.jpg'width = 60 height = 50 /> ;

labelimages.Text + =;



}

labelimages.Text + =点击查看所有股票;

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 += " <img src='image/delete.jpg'width=60 height=50/>";
labelimages.Text += "";

}
labelimages.Text += " Click to view All Stock ";

推荐答案

当您将路径指定为<$时c $ c> photos / your-image.jpg ,浏览器会解析相对于当前页面的路径。例如,如果当前页面是 http:// www。 yoursite.com/stock.aspx ,图像路径将被解析为 http://www.yoursite.com/photos/your-image.jpg - 服务器上的资源。



如果指定路径为 D:\ photos \your-image.jpg ,这是一个本地路径。浏览器将尝试从客户端计算机上的路径加载图像,它可能不存在;或者更可能是将阻止从互联网站点访问本地资源的尝试。



您在网页中包含的每个图像,脚本文件,样式表或其他外部资源必须从互联网地址加载。你不能使用本地路径。
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.


很可能在后台抛出异常。



这将与网站在iis下运行的帐户无法访问相关文件夹这一事实有关,因为它在网站文件夹之外



从内存(因为我无法访问我的图片)我相信你必须确保aspnet帐户有权访问
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


除了解决方案2。



很可能,即使在服务器的主机文件系统上也无法访问D:/ photo目录。

ASP.NET应用程序,与所有其他Web应用程序一样,通常在沙盒环境中执行,该环境不允许您访问任何本地目录,除非它位于为站点设置的rood目录下。即使此本地目录位于rood目录下,也不应使用绝对名称访问它。您需要使用相对URL或相对路径名(或相对于根)使用 MapPath ,它将名称转换为代码所需的绝对路径名:

https:// msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath%28v=vs.110%29.aspx [ ^ ]。



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


这篇关于从aspx页面中的c#代码显示本地驱动器路径中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆