Copy link to clipboard
Copied
I have a pdf file that downloads from a 3rd party vendor.
The issue is that the file does not have the .pdf extension and to work with the file we need to add the .pdf each time.
For example, a file downloads as abc but needs to renamed to abc.pdf
How can I rename the file correctly with the .pdf extension right after it downloads?
Copy link to clipboard
Copied
If you're serving the PDF from your site instead of the third-party vendor's site, you can use CFCONTENT to immediately provide a name and some other attributes to the file. But the key is you have to use CFCONTENT immediately after whatever you're doing to serve or generate the file. You can't have any whitespace between CFCONTENT and any previous CFML commands. For example, if you're using CFDOCUMENT to generate the PDF, you'd have to make sure there's no whitespace between your CFDOCUMENT tag and your CFCONTENT tag, kind of like this:
<cfdocument ...>...</cfdocument><cfcontent ...></cfcontent>
On the other hand, if the file is coming from the third-party vendor, you're probably going to have to fetch it yourself first via CFHTTP, then either rename the file on the server or use CFCONTENT or both - I'm not sure whether just renaming the file on the server is enough.
Dave Watts, Eidolon LLC
Copy link to clipboard
Copied
Could you share the code that you use for downloading the file?
In any case, here are 2 ideas (both assume you know the directory):
<cfset filepath="C:\downloads\testdoc">
<cffile action = "rename" source = "#filepath#" destination = "#filepath#.pdf">
done​
<cfset directorypath="C:\downloads">
<cfdirectory action="list" directory="#directorypath#" name="dirQuery">
<!---<cfdump var="#dirQuery#" >--->
<cfoutput query="dirQuery">
<!--- Loop across all files in the directory. If a file misses an extension, add '.pdf' to the name --->
<cfif type is "file" and listLen(name, ".") is 1>
<cffile action = "rename" source = "#directorypath#\#name#" destination = "#directorypath#\#name#.pdf">
</cfif>
</cfoutput>
done​
Find more inspiration, events, and resources on the new Adobe Community
Explore Now