4 Replies Latest reply on Dec 31, 2008 11:12 PM by 843785

    Download to Excel then delete the temp file

      I have succeeded in downloading data from my java application to an excel spreadsheet with multiple pages. That works fine. In doing this I write to a file. I now want to delete that file. I can get it to delete, but it happens before the user gets to download anything....or it does not delete.
      Here is the code I tried.

      //These are the variables I am using
      long sec = System.currentTimeMillis(); // set the time stamp
      String output="output."+sec+".xls"; // set the name of file

      //This is the section of code that I'm having trouble with. If I comment out the last line it does not delete the file. If I leave the last line in, it deletes before I can download and save anything.
      File newOutput = new File ("C:\\file_output\\", output);
      // Delete the temporary file now
      System.out.println( "Success of delete: " + newOutput.delete() );
        • 1. Re: Download to Excel then delete the temp file
          Yes, that last line of code does delete the file.

          But what's this "download" thing you're talking about? Misunderstanding how that works might be the source of your problem, so you should really explain what it is.
          • 2. Re: Download to Excel then delete the temp file
            Okay, yes you're right.
            The excel piece of code is:
                +workbook = Workbook.createWorkbook(new File("C:\\file_output\\"+output));+
            This reflects where the file will write out to....
            Then I have loads of code to set up the headings and what goes in each cell of the spreadsheet.
            Then I have
            then I have the download piece.
               Your file is now ready to be <A href="./file_output/<%=output%>"> downloaded </a>
            Immediately after that I have the code I originally listed.

            It seems I need to have a IF statement or something to tell it Only delete when the application is closed....but I don't know how to do that.
            • 3. Re: Download to Excel then delete the temp file
              Oh, I see. You are generating this file on the server, then sending a link to the client so that he or she can download the file.

              And your problem is that you want to wait until the client downloads the file before you delete it. But think about it: you send the link to the client. The client may have gone for lunch and may not download it for an hour. The client may have changed his/her mind and is NEVER going to download it.

              So, you can't have your code wait for the client to download the file. If you really want to persist in generating a file for the client to download, then your best bet is to not delete it in your code at all. Instead, have some process which runs periodically and deletes "download files" which are more than a couple of days old.

              But a better bet is to not generate a file at all. Instead, generate the spreadsheet and return that as the response, instead of returning HTML with a link. Then there aren't any files cluttering up your server. Google for "file download servlet" and you should find examples of how to do that.
              • 4. Re: Download to Excel then delete the temp file
                Excellent. I will try that. I could just run a daily cron job to delete the files if I don't find a better solution. Of course I have to look up how to do a cron job, lol.

                I'll do some more research.