简体   繁体   English

HTML img 和 ASP.NET 图像和相对路径

[英]HTML img and ASP.NET Image and relative paths

What is the correct way to reference an image in ASP.NET for live deployment on IIS?在 ASP.NET 中引用图像以在 IIS 上进行实时部署的正确方法是什么?

The following works in dev and production:以下在开发和生产中工作:

<asp:ImageButton ID="ibnEdit" runat="server" OnClick="ibnEdit_Click" ImageUrl="~/App_Themes/Default/images/one.png" Visible="false" ToolTip="Edit" />

The following doesn't work in either: (why not?)以下两种情况都不起作用:(为什么不呢?)

<img src="~/App_Themes/Default/images/two.gif" />

The following works in dev but not in production:以下适用于开发但不适用于生产:

<img src="../App_Themes/Default/images/two.gif" />

If you want to use a regular img tag with the ~ path, you can just add runat="server" into the tag as an attribute (like regular server controls) and the path will be resolved.如果您想使用带有~路径的常规img标签,您只需将runat="server"作为属性添加到标签中(如常规服务器控件),路径将被解析。 eg:例如:

<img src="~/App_Themes/Default/images/two.gif" runat="server" /> 

For your second part, is the ../ image reference appearing on more than one page, for example a user control or master page (etc) such that you might be using it at different folder levels...对于您的第二部分,../ 图像参考出现在多个页面上,例如用户控件或母版页(等),以便您可能在不同的文件夹级别使用它...

我使用此语法从母版页访问图像

<img src="<%=ResolveUrl("~/Content/Images/error_img.jp")%>" width="350" style="padding-right: 15px; padding-top: 20px;"/>

The ~ will only work on a server control such as <asp:Image> or <asp:ImageButton> . ~ 仅适用于服务器控件,例如<asp:Image><asp:ImageButton> This tells ASP.Net to insert the application path.这告诉 ASP.Net 插入应用程序路径。 Sometime that's just "/" but if your application is not the root directory of the website it will include the path it is in. The img tag is just html and it will not be altered by ASP.Net, so the browser gets the path "~/App_Themes/Default/images/two.gif" and doesn't know how to read it.有时这只是"/"但如果您的应用程序不是网站的根目录,它将包含它所在的路径img标签只是 html,它不会被 ASP.Net 更改,因此浏览器获取路径"~/App_Themes/Default/images/two.gif" ,不知道怎么读。

I don't know why the last example works in dev but not in production.我不知道为什么最后一个例子在开发中有效,但在生产中无效。 It might has something to do with having the application in the root directory in dev but in a sub directory in production.它可能与将应用程序放在 dev 的根目录中但在生产中的子目录中有关。

This worked for me这对我有用

$(".selector").attr('src', "Content/themes/base/images/img.png"); $(".selector").attr('src', "Content/themes/base/images/img.png");

The important is that you do not have "/" at the beginning of your new src.重要的是您的新 src 开头没有“/”。

byte[] imageArray = System.IO.File.ReadAllBytes(Server.MapPath("~/Images/Upload_Image.png"));

string base64ImageRepresentation = Convert.ToBase64String(imageArray);

You can look into my answer .你可以看看我的回答。 I have resolved the issue using C# language.我已经使用 C# 语言解决了这个问题。 Why can't I do <img src="C:/localfile.jpg">? 为什么我不能做 <img src="C:/localfile.jpg">?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM