Skip to main content
Hedge
Inspiring
Feb 16, 2018 9:46 PM UTC
Question

cfzip I need to pause page processing until the zip file is complete, suggestions?

I am creating zip files using cfzip and it is basically zipping up some high rez images. So after the cfzip code I have the following

 

<cfset TheFileName = "#ReReplace(GetImage.ItemNum, " ", "-", "ALL")#.zip"> 

<cfheader name="Content-disposition" value="attachment;filename=#TheFileName#">

<cfcontent type="application/zip, application/x-zip, application/x-zip-compressed, application/octet-stream, application/x-compress, application/x-compressed, multipart/x-zip" file="#APPLICATION.ProductImageDirectory#\full-brands\#TheFileName#">

 

The problem I am having is it starts the download of the zip file before it is finished so it downloads a corrupt zip file each time. The zip file on the server is fine. Any idea how I can get the page execution to pause until the zip file is actually finished being created?

This topic is closed to new replies. Start a new post to keep the conversation going.

24 replies

BKBK
Community Expert
Community Expert
Feb 18, 2018 11:02 AM UTC

Perfect use case for a named lock. Something like this, for example

<cflock name="unique_name_for_zip_operation_lock"  timeout="600">

<!--- The code that writes the ZIP file --->

</cflock>

The timeout is 10 minutes in my example. Adjust to your specific case.

Then, elsewhere

<cflock name="unique_name_for_zip_operation_lock"  timeout="30">

    <cfif fileExists(APPLICATION.ProductImageDirectory & "\full-brands\" & TheFileName)>

        <cfset TheFileName = "#ReReplace(GetImage.ItemNum, " ", "-", "ALL")#.zip">

        <cfheader name="Content-disposition" value="attachment;filename=#TheFileName#">

        <cfcontent type="application/zip, application/x-zip, application/x-zip-compressed, application/octet-stream, application/x-compress, application/x-compressed, multipart/x-zip" file="#APPLICATION.ProductImageDirectory#\full-brands\#TheFileName#">

    </cfif>

</cflock>

Translate
Hedge
HedgeAuthor
Inspiring
Feb 19, 2018 5:30 PM UTC

Ok first let me start by saying I found the problem and it was a simple mistake. I was creating the file to a directory called image-collections but then I was trying to download the file from a directory called full-brands. Sadly this is what happens when you copy and paste the code from other parts of your project and miss something like that.

However, I do have 2 questions as it relates to the cflock code you provided.

Does the name of the two separate cflocks need to match?

What is cflock intended to do in this use case? I have seen using it for session variables but not code like this so I am curious.

Translate
Community Expert
Feb 19, 2018 5:35 PM UTC

CFLOCK in general is intended to prevent two blocks of code that might change data from executing at the same time. You can have scope locks, which use the name of the scope to prevent two blocks of code using the same scope from executing, or named locks, which use some arbitrary name to do the same.

Dave Watts, CTO, Fig Leaf Software

Translate
Dave Watts, Eidolon LLC
Hedge
HedgeAuthor
Inspiring
Feb 16, 2018 10:57 PM UTC

Ok so I just tried this for testing. I pulled the page up that executes the code and on another monitor, I pulled up the directory via FTP. I hit enter to run the page and then immediately refreshed the ftp and the file was complete and full size. So the file is being created in like 0.3 seconds maybe. So could the issue be how it is requesting the file? So could this part of the issue be a problem?

<cfset TheFileName = "#ReReplace(GetImage.ItemNum, " ", "-", "ALL")#.zip">

<cfheader name="Content-disposition" value="attachment;filename=#TheFileName#">

<cfcontent type="application/zip, application/x-zip, application/x-zip-compressed, application/octet-stream, application/x-compress, application/x-compressed, multipart/x-zip" file="#APPLICATION.ProductImageDirectory#\full-brands\#TheFileName#">

I don't want them to leave the page to download the created file.

Translate
Community Expert
Feb 17, 2018 12:15 AM UTC

It could be that the file is locked by the FTP creation operation, too. I think you'll have to put some sort of delay in there one way or another to make this work.

Dave Watts, CTO, Fig Leaf Software

Translate
Dave Watts, Eidolon LLC
Hedge
HedgeAuthor
Inspiring
Feb 17, 2018 12:35 AM UTC

There isn't an FTP creation operation though. I just had filezilla open to check on how fast the file gets created.

Translate
WolfShade
Legend
Feb 16, 2018 10:07 PM UTC

How large are the zip files?  I'm using similar code for downloading Word and Excel files that are often over 1M in size, and I've never experienced the situation that you are describing.

Also, is this happening in more than one browser?

V/r,

^ _ ^

Translate
Hedge
HedgeAuthor
Inspiring
Feb 16, 2018 10:13 PM UTC

Zip file is anywhere from 4 - 15 mb. I have tried it in both Chrome and Firefox. They seem to download a file about 126kb and almost immediately upon telling the page to process so I feel like it is trying to download the file before it is finished being created.

Translate
Community Expert
Feb 16, 2018 10:05 PM UTC

You can use the sleep method to pause execution of the page. If you want to be more exact about this, you could use CFTHREAD to actually kick off the creation of the zip file, then have your main page periodically check for the existence of the zip file and put itself to sleep for a while if the zip file isn't ready yet. Then, you could redirect to the zip file, or to another page or view that would cause the zip file to be downloaded.

Dave Watts, CTO, Fig Leaf Software

Translate
Dave Watts, Eidolon LLC
Hedge
HedgeAuthor
Inspiring
Feb 16, 2018 10:16 PM UTC

cfsleep is not supported on official ColdFusion.

Translate
Community Expert
Feb 16, 2018 10:18 PM UTC

The sleep function is certainly supported by official ColdFusion.

Dave Watts, CTO, Fig Leaf Software

Translate
Dave Watts, Eidolon LLC