Seybsen
Seybsen

Reputation: 15581

ColdFusion: force browser to download file and still load the page

I have an test.cfm where I dynamically build a pdf-File and output it with <cfheader> and <cfcontent> to the browser, but I still want the page to load and show "test html":

<CFFILE action="readbinary" file="#expandpath("./test.cfm")#" variable="testcontent" />
<CFHEADER name="Content-Disposition" value="attachment; filename=""test.txt""; charset=utf-8">
<CFCONTENT type="text/plain" reset="yes" variable="#testcontent#">

<CFCONTENT type="text/html" reset="yes" /><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testdoc</title>
</head>
<body>test html</body>
</html>

(outputting the file itself is just for the example)

Is there a way to accomplish this?

Upvotes: 1

Views: 2247

Answers (1)

Peter Boughton
Peter Boughton

Reputation: 112160

You can't trigger what happens after a download, but you can do it the other way round - load the page you want, then redirect to the file to download using a HTML meta redirect:

<meta http-equiv="refresh" content="5; url=http://domain.com/path/to/download" />

(If necessary, you can use cfhtmlhead to insert that into the relevant part of an existing HTML page.)

The 5 is the number of seconds to wait - setting to 0 will redirect immediately. (When using with other pages, using an instant redirect can cause issues with the back button; should be less of an issue for downloads though.)

Upvotes: 1

Related Questions