[英]Adding src Dynamically to img in C#
我有一个img
标签。 (我只使用image而不是asp.net,因为它是我与img
一起使用的imageMap
)。 我通过以下3种不同方式动态设置src
。 第一种方法有效,并且图像包含在项目中。 但是,当我尝试以src
方式添加src
,它无法显示图像。 该图像不属于项目。 我什至尝试了将第三种方法用于其他位置,但也无法加载。 它也不是项目的一部分。 我需要做的是动态加载ans image src
,它不包含在项目中并显示出来。
<img runat="server" class="ImageMap" id="ImageMapID" src="" usemap="#imagemap" />
ImageMapID.Attributes["src"] = @"/Icons/XXXX XXXX.png";//first way that works
ImageMapID.Attributes["src"] = @"/documents/XXXXX/XXXX_04092017.pdf"; // second way that fails
ImageMapID.Attributes["src"] = @"file://C:/temp/XXXXXXXX.pdf";// third way that fails
有任何想法吗? 我应该添加它们是不同的图像,但是它们都应该起作用。
首先您应该知道,PDF文件不是图像对象,因此您不能使用img
标签嵌入它。 您需要改用iframe
代码:
<iframe src="file:///C:/temp/XXXXXXXX.pdf" style="width: 100%; height: 100%;" frameborder="0" scrolling="no">
...
</iframe>
或使用具有相同文件路径的embed
/ object
标签:
<embed src="file:///C:/temp/XXXXXXXX.pdf" width="600" height="400" type="application/pdf">
<object type="application/pdf" data="file:///C:/temp/XXXXXXXX.pdf" title="XXXXX" width="600" height="400" >...</object>
如果要包括图像的本地文件路径,请使用file
引用路径或具有正确目录结构的相对路径,如下例所示:
ImageMapID.Attributes["src"] = @"file:///C:/temp/Icons/XXXXXXXX.png";
// watch out for current relative path you're using
ImageMapID.Attributes["src"] = @"../../temp/Icons/XXXXXXXX.png";
请注意,Windows本地驱动器路径使用此格式来引用任何文件,以在具有source属性的任何HTML元素中使用:
file:///[drive letter]:/[path_to_resource]/[filename].[extension]
参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.