Skip to main content
New Participant
May 21, 2024
Question

Pipe a remote file to response

  • May 21, 2024
  • 1 reply
  • 1265 views

How can I pipe a remote file directly to the response, without having to read the file in the memory or writing it to disk on the server? The remote file can be huge and I'm not going to do anything to the file itself. If possible I'd like to be able to set the basic headers like 'content-type' and 'content-length'. 

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    May 22, 2024

    You can do it using three lines of code:

    <cfhttp  method="get" getasbinary="yes" result="httpResult" charset="utf-8" url="url_of_remote_file">
    <!--- Here, ".ext" stands for an arbitrary file extension. Specify the one you want ---> 
    <cfheader name="content-disposition" value="inline;filename=binaryfile.ext">
    <!--- Application/octet-stream is the default MIME type for binary files. ---> 
    <cfcontent type="application/octet-stream" variable="#httpresult.filecontent#">

    Example:

    <cfhttp  method="get" getasbinary="yes" result="httpResult" charset="utf-8" url="https://community.adobe.com/t5/image/serverpage/image-id/694431i786EBFDC18FC0D6D/image-size/original?v=v2&px=-1">
    <!--- I have used JPG extension because I know the URL points to a pivture --->
    <cfheader name="content-disposition" value="inline;filename=binaryfile.jpg">
    <cfcontent type="application/octet-stream" variable="#httpresult.filecontent#">

     

    charlieCFAuthor
    New Participant
    May 22, 2024

    @BKBK Thank you for your reply. 

    But isn't that cfhttp will hold the entire remote file in-memory? I have files in the hundreds of MB hence I'd like to avoid holding the files in-memory or writing them to disk. More like "piping" the remote file to the response directly. If CF can't do this, maybe I can tap on Java, but what package or library to use? TIA

    charlieCFAuthor
    New Participant
    May 22, 2024

    You could always ask ChatGPT to rewrite that Java code for use in ColdFusion. 🙂


    @sdsinc_pmascari That's a great idea 😀
    OMG AI is getting my job...