Reputation: 45
def exportExcel(){
if(!params.max) params.max = 10
if(params?.type && params.type != "html"){
response.contentType = grailsApplication.config.grails.mime.types[params.type]
response.setHeader("Content-disposition", "attachment; filename=agents.${params.extension}")
List fields = ["orgTypeId", "name"]
Map labels = ["orgTypeId":"DP Id", "name":"Name"]
def upperCase = { domain, value ->
return value.toUpperCase()
}
Map parameters = [title: "Agent List", "column.widths":[0.15, 0.4]]
Map formatters = [name: upperCase]
exportService.export(params.type, response.outputStream, Agent.list(params), fields, labels, formatters, parameters)
}
render(view: 'index',model: [organizationTypeCount: Agent.count(), organizationType: Agent.list(params)])
}
This is my code to export excel. When I click on export button. It show Failed-Network error. If I resume download it will be downloaded.
This is the error : java.lang.IllegalStateException: getOutputStream() has already been called for this response
Please help me resolve this issue.
Upvotes: 1
Views: 59
Reputation: 3370
You cannot write attachment data to the output stream, and then render the index view; that is trying to render your view to the same output stream that you already sent the attachment to. If you remove the line rendering the index view, your code should work as expected.
If you need to generate an attachment/download AND move to another view in your browser, you will need to send two requests to do that.
Upvotes: 1