0

I have migrated wicket 1.x to wicket 8.x.

I have added below code for excel file download but getting the first downloaded file in all other pages on excel download.

ResourceLink<Object> excelLink =  new ResourceLink<>("excel", new ResourceReference("downloadExcel") {
            private static final long serialVersionUID = 1L;

            @Override
            public IResource getResource() {
                byte [] exBytes = null;
                try {
                    exBytes = new byte[0]; // Some excel file into byte format
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return new ByteArrayResource(fileFormat.getContextType(), exBytes, fileName);
            }
        });
excelLink.setOutputMarkupId(true);
excelLink.add(new Label("excelLabel", new ResourceModel("excelLabel")));
return excelLink;

I am using the same excel download logic in all the other pages with same ResourceLink Id "excel" in all the pages with the same name of all the Excel files in all the pages in the application.

If in case it is maintaining the cache then how can clear the cache to download the correct excel file in each page?

Kindly let me know if anyone can help me to resolve this issue it will be more appreciable.

user3552342
  • 657
  • 1
  • 5
  • 14
  • Have you tried debugging getResource()? Is it executed each time you click the download button? If not have tried cleaning browser cache between two downloads? – Andrea Del Bene Jan 21 '21 at 21:46
  • Yes, it is executing each time whenever clicks on the download button. I have tried by clearing the cache but which file I download first that same file only will be getting download on other download clicks on other pages. In my case file name is same in the other pages also. – user3552342 Jan 22 '21 at 09:43

2 Answers2

2

To disable caching for this resource you could do:

 return new ByteArrayResource(fileFormat.getContextType(), exBytes, fileName) {
   @Override 
   protected void configureCache(final ResourceResponse data, final Attributes attributes) {
       data.setCacheDuration(Duration.NONE);
       super.configureCache(data, attributes);
   }
 };
martin-g
  • 17,243
  • 2
  • 23
  • 35
  • I tried with the above code but it didn't work for my case. In my case file name is same in the other pages also. – user3552342 Jan 22 '21 at 09:43
  • You can use different file names if this is a requirement for you. My code just sets the proper response headers which disable the caching. You may need to clear out your browser cache that is already populated before my code. – martin-g Jan 22 '21 at 12:15
  • I have added the timestamp in the file name in order to differ the file name on each download click. This resolved my issue as in all pages file name is same. Thanks. – user3552342 Jan 26 '21 at 14:37
  • Hi @martin, please vote for this question. Hope this can help others to resolve their issues. – user3552342 Mar 12 '21 at 07:45
-1

Above code is working fine to return the excel file. Here I have found the issue with the excel file name where the name of the excel file is same in all the pages of my application as it was implemented earlier in the previous version of Wicket and it was working fine before. But after wicket migration from 1.x to 8.x version it is returning the old downloaded excel file on click of download excel file. So now I have added the timestamp in the file name in order to keep the different file name on each page for excel download.

Example: Before the file name was "UserData.xls" and now after adding the timestamp in file name "UserData_10022021_021311.xls" (UserData_ddMMyyyy_HHmmss.xls). This resolves my issue for my use case.

I hope it can help someone who also facing the same issue.

user3552342
  • 657
  • 1
  • 5
  • 14