Skip to main content
Inspiring
May 13, 2006
Answered

Downloading files to user

Hello!

I am using Flex front end in conjunction with CF backend to create a file, then download it to the user. The series of events is:

Flex calls the first CF page using LoadVars:
my_lv.send("filemaker.cfm","new1","POST");"

The filemaker runs a query and creates a file on the server, then calls a download page, passing it the file name like so:
<SCRIPT language="JavaScript1.2" type="text/javascript">
window.open("sendfiletouser.cfm?outputfile=<CFOUTPUT>#URLEncodedFormat(OutputFile)#</cfoutput>","new1");
</SCRIPT>

The sendfiletouser.cfm basically uses a CFHEADER/CFCONTENT combo to squirt the file to the user (obviously I set the variables properly)
<CFHEADER name="Content-Disposition" value="attachment; filename=#FileName#">
<CFCONTENT type="#MimeType#" file="#OutputFile#" deletefile="yes">

It works GREAT! Files go out exactly how they need. Except….

There is a window left behind. Yuck. I don't want to over-write the Flex ap running so I need a new window. I could use frames, creating a zero height frame to "hold" the new1 window name but I really don't want to do that. I can't write out code following the cfcontent so.....

Does anyone know how I can fix this? Or another way of sending a file out to the user from CF without leaving a window behind?

Cheers!

This topic is closed to new replies. Start a new post to keep the conversation going.
wrkochCorrect answer
Inspiring
June 22, 2006
No and yes. I was unable to find any way to close the window. I tried more techniques than you can imagine. Even rewrote the download to use JAVA. No luck. But...

I did solve the problem. I noticed that the Flex generated html code was using a zero height IFRAME to hold the history data. I just added another named IFRAME and pointed the loadvars.send at it. Works great!

27 replies

Inspiring
June 21, 2006
any news?
Translate
wrkochAuthorCorrect answer
Inspiring
June 22, 2006
No and yes. I was unable to find any way to close the window. I tried more techniques than you can imagine. Even rewrote the download to use JAVA. No luck. But...

I did solve the problem. I noticed that the Flex generated html code was using a zero height IFRAME to hold the history data. I just added another named IFRAME and pointed the loadvars.send at it. Works great!
Translate
Inspiring
May 23, 2006
if it's anything like FLASH/actionscript:

(better to dynamically create the paramters but quick example)

getURL("javascript:window.open('filemaker.cfm','new1','height=400,width=400
,toolbar=no,scrollbars=no,statusbar=no,directories=no');");
Translate
wrkochAuthor
Inspiring
May 23, 2006
Yup -- just have to figure out the the proper syntax to get the variables to transfer under a POST. I want to keep them hidden.
Translate
Inspiring
May 23, 2006
really should use a form with hidden fields then

(the filemaker.cfm could submit hidden fields - but this may be too much of a change in your logic)


Translate
wrkochAuthor
Inspiring
May 17, 2006
Nag screen went away but the windows (two of them) don't close.
Translate
Inspiring
May 17, 2006
Just to be clear, use either/or of these, don't use all seven lines:

window.opener=null;
opener.close();
window.close();

-or-

window.opener = top;
window.opener=null;
opener.close();
window.close();
Translate
wrkochAuthor
Inspiring
May 17, 2006
Right.

I tried

<cfif IsDefined("URL.called")>
<SCRIPT language="JavaScript1.2" type="text/javascript">
while(window.opener != null && !window.opener.closed){
window.opener=null;
opener.close();
window.close();
}
</script>
</cfif>

AND

<cfif IsDefined("URL.called")>
<SCRIPT language="JavaScript1.2" type="text/javascript">
while(window.opener != null && !window.opener.closed){
window.opener = top;
window.opener=null;
opener.close();
window.close();
}
</script>
</cfif>
Translate
Inspiring
May 16, 2006
Try adding the last two window lines and see if it works:

<SCRIPT language="JavaScript1.2" type="text/javascript">
window.open("sendfiletouser.cfm?outputfile=<CFOUTPUT>#URLEncodedFormat(OutputFile)#</cfoutput>","new1");
window.opener=null;
window.close();"
</SCRIPT>
Translate
wrkochAuthor
Inspiring
May 16, 2006
Nope, that shut the window the instant it loaded, giving no time for the user to download the file.

The filemaker.cfm creates a file on the server. The sendfiletouser.cfm uses CFHEADER/CFCONTENT to download it to the user.

I guess what I'm looking for is a window event that I can trap when the download is complete or the user cancels. Or a JAVA way of doing this so I can avoid CFCONTENT.
Translate
Inspiring
May 16, 2006
Beauty of CFCONTENT is that you can delete the file after you serve it, I understand. I think I may have the answer.

Add the 'openercheck' line to your JavaScript in your filemaker.cfm file.

<SCRIPT language="JavaScript1.2" type="text/javascript">
window.open("sendfiletouser.cfm?outputfile=<CFOUTPUT>#URLEncodedFormat(OutputFile)#</cfoutput>","new1");
openercheck = window.open("open_check.cfm?called=y","closeall","width=0,height=0,x=-2000,y=-2000");
</SCRIPT>

Create a file in the same directory called open_check.cfm consisting of:

<cfif IsDefined("URL.called")>
<SCRIPT language="JavaScript1.2" type="text/javascript">
while(window.opener != null && !window.opener.closed){
opener.close();
window.close();
}
</script>
</cfif>

How this should work is 'openercheck' will be a window that will monitor 'new1' since 'new1' is it's opener. I sized 'openercheck' window small and placed it off the viewable browser space so user will not see it. The 'openercheck' window will continually monitor the state of it's opener (new1) and keep trying to close it (it will be unable to close it while the file download prompt is displayed to the user; when the user clicks Save, the 'openercheck' window will have the opportunity to close it's opener (new1) and itself. But the file save screen will still be active so the user can finish downloading the file.

You will notice the open_check.cfm?called=y line, this is to let open_check.cfm know it WAS called. So if someone runs open_check.cfm via mysite.com/open_check.cfm it will not execute the JavaScript; since the script is a continuous loop.

Since 'new1' is a spawned window via your .send command, we should be able to close it. Hopefully, this will work. Let me know either way if you can. I've only tested in IE6.
Translate
wrkochAuthor
Inspiring
May 16, 2006
Good part is it does close the window. Bad part is it pops up a message asking wheter or not the user wants to close the window. If they pick yes, then no download. If no, the window is still left behind. I added more time to the timeout which helped.

I think you are are on the right track. What would be really good is if the close method could be fired when the download is complete. I don't know if that event is available......
Translate
Inspiring
May 16, 2006
Are you serving an actual file on the server to the user or using CFCONTENT to serve dynamically built data?
Translate
Inspiring
May 15, 2006
Try this:

<SCRIPT language="JavaScript1.2" type="text/javascript">
var fileWin = window.open("sendfiletouser.cfm?outputfile=<CFOUTPUT>#URLEncodedFormat(OutputFile)#</cfoutput>","new1",'toolbars=no,resizeable=no,width=10,height=10,left=-20,top=-20');
setTimeout("fileWin.close()",3000);
</SCRIPT>

I added some more params to your popup in case you wanted to hide it

jam
Translate
Inspiring
May 15, 2006
You should not need the popup window. Just call the sendfiletouser.cfm file directly from the first page. The CFHEADER/CFCONTENT will spawn the download prompt window. Try this as an example:

<form method="post" action="##">
<input type="text" name="enc_key" value="123">
<input type="submit">
</form>

<cfif IsDefined("Form.enc_key")>
<cfheader name="Content-Disposition" value='attachment; filename="key.key"'>
<cfcontent type="unknown"><cfoutput>#Form.enc_key#</cfoutput>
</cfif>
Translate
wrkochAuthor
Inspiring
May 15, 2006
It's not the filemaker.cfm that is causing the problem. It is the loadvars. The send method creates a window called "new1". The window.open in filemaker writes to this window. The CFCONTENT (wherever it is) prevents me from writing out code to close the "new1" window.

Trick would be to download the file automatically without using CFHEADER/CFCONTENT so control of the window could be retained. I was thinking maybe some JavaScript or even a Java process (I am not Java literate but I bet there would be a way)
Translate