http://www.perlmonks.org?node_id=1049575


in reply to cgi download and delete file

You know that Private SNAFU is always on duty:   the mere fact that you zipped-up a bunch of files and pushed them to an HTML stream doesn’t mean that they arrived, any more than it does at the Post Office.   I would therefore design this program to build the Zip files, then remove the files from whence they were made (if appropriate), then put that file into a “to be downloaded” directory under some appropriate name.   From these, when the user requests a download, the file is renamed in some slight way to indicate that it “has been or is-being downloaded.”   (Renaming ahead-of-time avoids any file contention issues.   Moving it to a different directory ahead-of-time does the same thing.)   But in every step you have thus given yourself a way out.   If the download did not for whatever reason succeed, the data is still there and the process could be repeated.

Replies are listed 'Best First'.
Re^2: cgi download and delete file
by teddyttas (Novice) on Aug 15, 2013 at 14:35 UTC
    Thank you very much! Anyway, I think there is something I am getting wrong... When I use the cgi::application's header_prop (in the download_file subroutine) what I'm actually doing is a redirection to the download window, is it right? Cause I tried to make a redirection after the execution of the subroutine, but in this case the download doesn't start. If I put a sleep command after the subroutine, the script executes before the sleep command and then launches the download. So...I don't really understand this behaviour. I tried :
    $self->download_zip(); sleep 5; my $filename = "temporaneo/" . $self->session->param("nom_projet") + . ".zip"; unlink $filename;
    and
    $self->generate_zip(); $self->download_zip(); return $self->redirect("http://delete.cgi");
    What I was trying to do was following your advice: put the zip file in another directory and, if succeed, redirect to another run mode that erases the file, otherwise stay on the page and relaunch the download.
      Okay, I think I got the solution to my last question... it was simple. In fact I solved it in the following way...
      $self->generate_zip(); my $output = $self->download_zip(); #here I can do what I want. unlink and so on.. my $filename = "temporaneo/" . $self->session->param("nom_projet") + . ".zip"; unlink $filename; #and only then I stream the output return $output;
      I can now redirect to another runmode passing the $output variable and solve the problem as you suggested. thank you very much!