Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Add file extension to downloaded filename

Participant ,
Dec 12, 2022 Dec 12, 2022

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?


189
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2022 Dec 12, 2022

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

Dave Watts, Eidolon LLC
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 13, 2022 Dec 13, 2022
LATEST

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

  • per downloaded file
    <cfset filepath="C:\downloads\testdoc">
    <cffile action = "rename" source = "#filepath#" destination = "#filepath#.pdf">
    done​
  • per download directory (containing a number of files missing the pdf extension)
    <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​

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources