[
https://issues.apache.org/jira/browse/WICKET-5826?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14297637#comment-14297637
]
Martin Grigorov commented on WICKET-5826:
-----------------------------------------
I guess these different links will link to different files, i.e. files with
different extensions.
As Sven explained you can configure your application globally to use specific
mime type for specific extension.
> 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
> Assignee: Sven Meier
> Priority: Minor
> Labels: easyfix, newbie
> Original Estimate: 0.25h
> Remaining Estimate: 0.25h
>
> 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)