Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wicket: downloading files after an onSubmit

So what I have is a DataView with three columns, one of which is a checkbox column that allows users to check which files they would like to download.

For simplicity's sake (I think); I've decided to compress the files into a single zip file and serve it after it's generated.

Here's what I have so far:

Code::


    Button downloadLogButton = new Button("downloadlogbutton") {
        private static final long serialVersionUID = 1L;
        @Override
        public void onSubmit() {
            // Some utility class I made that zips files
            LogUtility util = new LogUtility();
            util.zipLogFiles("sample", logs);
        }
    };

    Form logsForm = new Form("logsform") {

    };

    logsForm.add(downloadLogButton);

    CheckGroup<File> checkGroup = new CheckGroup<File>("logscheckgroup", new ArrayList<File>());
    WebMarkupContainer logsViewContainer = new WebMarkupContainer("datatable");
    DataView<File> logsView = new DataView<File>("logrows", new ListDataProvider<File>(logs)) {

        private static final long serialVersionUID = 1L;

        public void populateItem(final Item<File> item) {
            final File file = (File) item.getModelObject();
            item.add(new Check<File>("logdownloadcheck", item.getModel()));
            item.add(new Label("logname", file.getName()));
            SimpleDateFormat sdf = new SimpleDateFormat( "E, dd MMM yyyy  hh:mm a");
            item.add(new Label("logdatemodified", sdf.format(file .lastModified())));
        }
    };

    logsViewContainer.add(logsView);
    checkGroup.add(logsViewContainer);
    logsForm.add(checkGroup);
    add(logsForm);

How do I serve the zip file after it is generated for download? What are my options? I'd like to avoid having to redirect them to a confirmation page or a Your download is ready page.

UPDATE

Based on Xavi López answer, I added the following code in my Button's onSubmit function.

org.apache.wicket.util.file.File log = new org.apache.wicket.util.file.File("/home/keeboi/Downloads/sample.zip");

IResourceStream resourceStream = new FileResourceStream(log);
IRequestHandler target = new ResourceStreamRequestHandler(resourceStream);

getRequestCycle().scheduleRequestHandlerAfterCurrent(target);

And I'm getting HTTP Error 404: Not Found.

like image 456
Kevin D. Avatar asked Jan 11 '13 09:01

Kevin D.


1 Answers

You could do just like DownloadLink does and create a FileResourceStream from the zipped file. Then, just change the target of the current request cycle:

Button downloadLogButton = new Button("downloadlogbutton") {
    private static final long serialVersionUID = 1L;
    @Override
    public void onSubmit() {
        // Some utility class I made that zips files
        LogUtility util = new LogUtility();
        util.zipLogFiles("sample", logs);
        IResourceStream resourceStream = new FileResourceStream(
            new org.apache.wicket.util.file.File(someFile)); // Use whatever file you need here
        IRequestTarget t = new ResourceStreamRequestTarget(stream){
            @Override
            public String getFileName() {
                return "filename.zip";
            }
        }
        getRequestCycle().setRequestTarget(t);
    }
};

If you wanted to delete the file after the download, you could override IRequestTarget#respond(RequestCycle) like this:

@Override
public void respond(RequestCycle requestCycle) {
    super.respond(requestCycle);
    // Delete the file
    ((FileResourceStream)getResourceStream()).getFile().delete();
}

The following related question could also be useful: How to use Wicket's DownloadLink with a file generated on the fly?.

like image 127
Xavi López Avatar answered Sep 24 '22 16:09

Xavi López