0

I have an img tag. (I am using just image and not asp.net because it is an imageMap that I have working with an img). I dynamically set the src as below in 3 different ways. The first way works and the image is included in the project. But when I try to add the src the 2nd way it fails to display the image. That image is not part of the project. I have even tried the 3rd way for a different location but it fails to load as well. It is not part of the project either. What I need to be able to do is load ans image src dynamically that is not included in the project and have it display.

<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

Any ideas anybody? I should add they are different images but they should all work.

Darren
  • 1,352
  • 5
  • 19
  • 49

1 Answers1

1

First thing you should know, PDF files are not image objects, so you can't use img tag to embed it. You need to use iframe tag instead:

<iframe src="file:///C:/temp/XXXXXXXX.pdf" style="width: 100%; height: 100%;" frameborder="0" scrolling="no">
   ...
</iframe>

Or use embed/object tag with same file path:

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

If you want to include local file path for images, use file reference path or relative path with proper directory structure shown in example below:

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";

Note that Windows local drive path uses this format to refer any files for use in any HTML elements with source attribute:

file:///[drive letter]:/[path_to_resource]/[filename].[extension]

References:

How to properly reference local resources in HTML?

Recommended way to embed PDF in HTML?

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • I just changed the document to a jpeg. That solved my problem. The pdf was breaking my app. Thanks for the lesson. I now know that pdf is not an image object. – Darren Sep 04 '17 at 02:44