Reputation: 579
I have following controller to download excel file. The file is downloaded but when I open it I get "The file is corrupt and cannot be opened.". What I'm doing wrong in my controller?
@ApiOperation(value = "export ontology")
@RequestMapping(value = "/export/{ontologyId}", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> exportExcel(@PathVariable Long ontologyId) throws FileNotFoundException {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Description", "File Transfer");
headers.add("Content-Disposition", "attachment; filename=pfizer_polish_ontology.xlsx");
headers.add("Content-Transfer-Encoding", "binary");
headers.add("Connection", "Keep-Alive");
headers.setContentType(
MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
File file = new File("C:\\ttt\\pfizer_polish_ontology.xlsx");
InputStreamResource isr = new InputStreamResource(new FileInputStream(file));
return ResponseEntity.ok().contentLength(file.length()).headers(headers).body(isr);
}
I want to add maybe a important detail I'm using Swagger interface to REST APIs
Using POSTMAN (Save and Download) I was able to download the file and it's good. So seems the problem is only when using swagger interface
Upvotes: 3
Views: 13641
Reputation: 44368
I bet your content type is not matching the Excel format. The followin should be enough:
contentType("application/csv")
Anyway, I suggest you to take a look on the webpage describing shirtly the MIME Types. As far as I see the format xsl(x) would match the best all of these:
application/excel
application/vnd.ms-excel
application/x-excel
application/x-msexcel
Upvotes: 1
Reputation: 5383
This is the one that I used for xlsx document:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Upvotes: 2