图像控制从路径加载 [英] Image control load from path

查看:68
本文介绍了图像控制从路径加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



在asp.net中,为什么图像控制只从应用程序路径加载。



谢谢

Basit。



我的尝试:



我试过



Image1.ImageUrl =Image / AnyImage.bmp



以上代码工作becoz图像文件夹在特定路径中



以下代码无效。

Image1.ImageUrl =D:\Images \ AnyYmage.bmp



如何实现



谢谢

Basit。

解决方案

如果这是真正的ASP.NET,那么它是基于网络的 - 这意味着你的路径必须相对于你的网站主文件夹,或者它们是相对于您的用户正在查看的页面。由于将所有页面保留在根文件夹中并不是一个好主意,因此应使用〜/ Image / AnyImage.bmp指定文件夹并调用Server.MapPath将其转换为正确路径。直接访问网站上的D盘是个坏主意,因为网络主机甚至可能没有! :笑:

如果您只是随机标记内容并且这是一个Windows应用程序,那么可能是因为权限:Images文件夹是否为应用程序运行的用户提供了合适的权限? / blockquote>

因为ImageUrl属性只是定义了image元素的src标记中的内容。因此,如果您的页面是www.mysite.com/并且您将ImageUrl设置为/images/mypic.jpg,则会创建以下html并将其发送到浏览器



 <   img     src   =  / images / mypic .jpg  /  >  





当浏览器完成加载html后,它将处理这样的外部资源,然后您的浏览器将从您的服务器请求www.mysite.com/images/mypic.jpg将生成的图像放在img标签所在的位置。



如果将ImageUrl设置为d:\ mypic.jpg,则标签看起来像



 <   img     src   =  d:\ mypic.jpg />  





然后浏览器会尝试从客户端加载图像-drive,但图像在您的服务器上,而不是客户端PC。


Dear,

In asp.net why image control only load from application path only.

Thanks
Basit.

What I have tried:

I tried

Image1.ImageUrl = "Image/AnyImage.bmp"

above code working becoz image folder in specific path

below code is not working.
Image1.ImageUrl = "D:\Images\AnyImage.bmp"

How to achieve that

Thanks
Basit.

解决方案

If this is truly ASP.NET, then it's web based - and that means that your paths have to relative to your website home folder, or they are relative to the page your user is viewing. Since it's not a good idea to keep all pages in the root folder, you should specify the folder by using "~/Image/AnyImage.bmp" and calling Server.MapPath to convert it to a "proper" path. Directly accessing the D drive on a website is a bad idea, as the web host may not even have one! :laugh:
If you are just tagging things at random and this is a windows application, then it's probably because of permissions: does the Images folder give suitable permission to the user the app is running as?


Because ImageUrl property simply defines what is in the src tag of the image element. So if your page is www.mysite.com/ and you set the ImageUrl to "/images/mypic.jpg" then the following html is created and sent to the browser

<img src="/images/mypic.jpg"/>



When the browser finishes loading the html it will then process the external resources like this one, and your browser will then request www.mysite.com/images/mypic.jpg from your server and put the resulting image where the img tag is.

If you set the ImageUrl to "d:\mypic.jpg" then the tag would look like

<img src="d:\mypic.jpg/>



the browser would then try and load the image from the client's hard-drive, but the image is on your server, not the client PC.


这篇关于图像控制从路径加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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