Downloading same excel file in other wicket pages

324 views Asked by At

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.

2

There are 2 answers

0
user3552342 On BEST ANSWER

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.

4
martin-g On

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);
   }
 };