Using cfimage to display a file that doesn't h

2019-08-08 13:06发布

问题:

curious one this.

I'm working on a process that generates PDF files, combining data from various sources. The last piece of this process I need to complete is merging in image files.

This is actually fairly straightforward but the problem I have is the image files aren't stored with file extensions. Locally, I can change the filename, but in production this isn't an option.

So because a filename looks like : B71637CB-A49C-0653-EF813918736BDEB7

This will not work:

<cfimage action="writeTobrowser" source="#FilePath#> 

Same with

<img src="#FilePath#">.

So, any ideas on how I can work around this? Here's the code in context:

<cfdocument format="PDF" name="report" filename="#fileToDownloadimage#" overwrite="yes">
    <cfdocumentsection>
        <cfimage action="writeTobrowser" source="#FilePath#.jpg">
    </cfdocumentsection>
</cfdocument>

回答1:

If you need to embed the images into the PDF document, try HTML's inline image capabilities:

<cfset fileLocation         = "/path/to/images/B71637CB-A49C-0653-EF813918736BDEB7">
<cfset imageContent         = fileReadBinary(fileLocation)>
<cfset imageContentAsBase64 = toBase64(imageContent)>

<cfoutput>
    <img src="data:image/jpeg;base64, #imageContentAsBase64#" />
</cfoutput>


回答2:

So here's what ended up working:

<cfdocument format="PDF" name="report" filename="#fileToDownloadimage#" overwrite="yes">
    <cfdocumentsection>
        <cfset fileObject = fileReadBinary('#FilePath#') />
        <cfset imageObject = imageNew(fileObject) />
        <cfimage action="writeTobrowser" source="#imageObject#">
    </cfdocumentsection>
</cfdocument>

Alex's answer got me down the right path so I'm perfectly happy to leave the kudos in place, cos I wasn't getting anywhere near this!



回答3:

You can try creating a cfm page that outputs your content using cfcontent as in:

<cfcontent type="image/jpg" file="path/#fielpath#">

Then you would you include THAT cfm page as the source for your image as in

<img src="myFancyImageOuputer.cfm?image=#filepath#">

This should work but it may require some trial and error. :)

Ray has some additional tips here:

http://www.raymondcamden.com/2007/09/14/Serving-up-CFIMages-via-Image-Tags-and-a-NonCF-Friday-contest