Using cfimage to display a file that doesn't have an extension

随声附和 提交于 2020-01-06 01:44:29

问题


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



来源:https://stackoverflow.com/questions/34316662/using-cfimage-to-display-a-file-that-doesnt-have-an-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!