Skip to main content
Hedge
Inspiring
February 16, 2018
Question

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

  • February 16, 2018
  • 4 replies
  • 1715 views

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 has been closed for replies.

4 replies

BKBK
Community Expert
Community Expert
February 18, 2018

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>

Hedge
HedgeAuthor
Inspiring
February 19, 2018

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.

Community Expert
February 19, 2018

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

Dave Watts, Eidolon LLC
Hedge
HedgeAuthor
Inspiring
February 16, 2018

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.

Community Expert
February 17, 2018

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

Dave Watts, Eidolon LLC
Hedge
HedgeAuthor
Inspiring
February 17, 2018

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

WolfShade
Legend
February 16, 2018

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,

^ _ ^

Hedge
HedgeAuthor
Inspiring
February 16, 2018

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.

Community Expert
February 16, 2018

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

Dave Watts, Eidolon LLC
Hedge
HedgeAuthor
Inspiring
February 16, 2018

cfsleep is not supported on official ColdFusion.

Community Expert
February 16, 2018

The sleep function is certainly supported by official ColdFusion.

Dave Watts, CTO, Fig Leaf Software

Dave Watts, Eidolon LLC