Craig Jorgensen created WICKET-5826:
---------------------------------------

             Summary: Add setContentType to DownloadLink
                 Key: WICKET-5826
                 URL: https://issues.apache.org/jira/browse/WICKET-5826
             Project: Wicket
          Issue Type: Improvement
          Components: wicket
    Affects Versions: 6.18.0
            Reporter: Craig Jorgensen
            Priority: Minor


I am generating an excel file when the DownloadLink is clicked on.  Everything 
worked fine when I was running locally for development (meaning Excel would 
automatically start and my excel file would be loaded) but when I used our 
webdev server, the file would get appended with an ".htm" and the browser would 
try to display the excel file which resulted in garbage on the screen.

I eventually discovered that the contentType for the FileResourceStream in the 
DownloadLink::onClick() method was set to null and there is no way (that I know 
of to change it).  I modified the DownloadLink slightly to allow this change.  
If there is a better way to handle this, I would like to know it.  Otherwise, I 
would suggest adding my changes to the code.

Basically, I added a private member variable.

        private String contentType = null;

to the DownloadLink class with a getter and a setter and modified the onClick() 
method to allow me to set the FileResourceStream ContentType.

        @Override
        public void onClick()
        {
                final File file = getModelObject();
                if (file == null)
                {
                        throw new IllegalStateException(getClass().getName() +
                                " failed to retrieve a File object from model");
                }

                String fileName = fileNameModel != null ? 
fileNameModel.getObject() : null;
                if (Strings.isEmpty(fileName))
                {
                        fileName = file.getName();
                }

                fileName = UrlEncoder.QUERY_INSTANCE.encode(fileName, 
getRequest().getCharset());

                IResourceStream resourceStream = new FileResourceStream(new 
org.apache.wicket.util.file.File(file)) {
                        @Override
                        public String getContentType() {
                                return contentType;
                        }
                };
                getRequestCycle().scheduleRequestHandlerAfterCurrent(
                        new ResourceStreamRequestHandler(resourceStream)
                        {
                                @Override
                                public void respond(IRequestCycle requestCycle)
                                {
                                        super.respond(requestCycle);

                                        if (deleteAfter)
                                        {
                                                Files.remove(file);
                                        }
                                }
                        }.setFileName(fileName)
                                
.setContentDisposition(ContentDisposition.ATTACHMENT)
                                .setCacheDuration(cacheDuration));
        }





--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to