0

We have a download file link written using Wicket, and despite our best efforts, we cannot get it so the file can be downloaded with spaces... the file name is always encoded such that the spaces are converted to plus '+' signs.

Example: "My File.xls" is getting converted when downloaded to "My+File.xls"

See below for the code that we are using. We have tried playing with several options here, but the problem persists. Hoping someone help us narrow our search on what the problem area is.

Also, the resourceStream.downloadLink.prepareFileName() returns with "My File.xls" here, with the quotes excluded. We tried wrapping the filename with quotes which did not seem to work.

 public DownloadLinkListActionCell(String componentId, IModel<DownloadLink> rowModel) {
    super(componentId, rowModel);

    downloadLink = rowModel.getObject();

    IModel fileModel = new AbstractReadOnlyModel(){
        public Object getObject() {
            return generateFile();
        }
    };

    Link downloadFileLink = new Link("download") {
        @Override
        public void onClick() {
            File downloadFile = (File) fileModel.getObject();
            IResourceStream resourceStream = new FileResourceStream(
                    new org.apache.wicket.util.file.File(downloadFile));
            getRequestCycle().scheduleRequestHandlerAfterCurrent(
                new ResourceStreamRequestHandler(resourceStream,downloadLink.prepareFileName())
                {
                    @Override
                    public void respond(IRequestCycle requestCycle)
                    {
                        super.respond(requestCycle);
                    }
                }.setContentDisposition(ContentDisposition.ATTACHMENT)
                            .setCacheDuration(Duration.NONE)
            );
            setResponsePage(DownloadLinkPage.class, PageParametersBuilder.uniqueId(downloadLink.getId()));
        }
    };

}
gdbj
  • 16,102
  • 5
  • 35
  • 47

1 Answers1

1

According to When to encode space to plus (+) or %20? You need adjust

that + means a space only in application/x-www-form-urlencoded content

you need adjust your encode in header.

Qingfei Yuan
  • 1,196
  • 1
  • 8
  • 12
  • Ok. Would you know where we would set that in the above? We tried doing this in the respond() method above, but it didn't seem to work. – gdbj Jun 05 '19 at 17:45
  • try this: https://stackoverflow.com/questions/4397211/how-to-set-custom-http-response-header-in-wickets-ajax-responses – Qingfei Yuan Jun 05 '19 at 18:13