Coder Social home page Coder Social logo

Comments (7)

mariuszpala avatar mariuszpala commented on July 28, 2024

Just tried in Safari on Mac and it is the same:
image

from spring-framework.

snicoll avatar snicoll commented on July 28, 2024

@mariuszpala something else must be going on than the code snippet you've shared as I am unable to reproduce what you've described. I can see the header as the one you've shared but the browser saves the file with the expected name for me, that is dev_document.xlsx. Before we consider making a change, we need to understand the difference.

Please share a small sample we can run (not code in text) with instructions. I am also on Mac FWIW. Thanks.

from spring-framework.

mariuszpala avatar mariuszpala commented on July 28, 2024

OK, here is the more clear code snippet, I can prepare a complete sample app next week.

@RequestMapping(value = "/export", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	public ResponseEntity<Resource> exportToExcel() throws Exception {
		String fileName = "dev_document.xlsx";

		File xlsx = new File(fileName);
		InputStreamResource resource = new InputStreamResource(new FileInputStream(xlsx));

		String contentDisposition = ContentDisposition.attachment()
				.filename(fileName, StandardCharsets.UTF_8)
				.build()
				.toString();

		return ResponseEntity.ok()
				.contentLength(xlsx.length())
				.contentType(MediaType.parseMediaType(CONTENT_TYPE_XLSX))
				.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
				.header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION)
				.body(resource);
	}

from spring-framework.

snicoll avatar snicoll commented on July 28, 2024

That doesn't reproduce it either.

from spring-framework.

mariuszpala avatar mariuszpala commented on July 28, 2024

I am still unable to understand why I cannot reproduce it in a sample. I have exactly the same Spring Boot version, same endpoints returning same headers but it works from my sample, but doesn't work in our application.

@RequestMapping(value = "/export", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,produces = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	public ResponseEntity<Resource> exportToExcel() throws Exception {
		String sourceFile = "dev_document.xlsx";
		String fileName = "dev_document.xlsx";

		File xlsx = new File(sourceFile);
		InputStreamResource resource = new InputStreamResource(new FileInputStream(xlsx));

		String contentDisposition = ContentDisposition.attachment()
				.filename(fileName, StandardCharsets.UTF_8)
				.build()
				.toString();


		return ResponseEntity.ok()
				.contentLength(xlsx.length())
				.contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
				.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition)
				.header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION)
				.header("Strict-Transport-Security", "max-age=63072000; includeSubDomains; preload")
				.header("Referrer-Policy", "same-origin")
				.header("X-Content-Type-Options", "nosniff")
				.header("X-Frame-Options", "SAMEORIGIN")
				.header("Vary", "Origin")
				.header("Vary", "Access-Control-Request-Method")
				.header("Vary", "Access-Control-Request-Headers")

				.body(resource);
	}

This is the list of headers from my app where it's not working:

HTTP/1.1 200
Date: Thu, 28 Mar 2024 07:30:12 GMT
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Content-Length: 118326
Content-Disposition: attachment; filename="=?UTF-8?Q?dev=5Fdocument.xlsx?="; filename*=UTF-8''dev_document.xlsx
Connection: keep-alive
Referrer-Policy: same-origin
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Access-Control-Expose-Headers: Content-Disposition
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload

image

And this is the list of response headers from the sample above when it's working:

HTTP/1.1 200
Date: Thu, 28 Mar 2024 07:27:02 GMT
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Content-Length: 16347
Content-Disposition: attachment; filename="=?UTF-8?Q?dev=5Fdocument.xlsx?="; filename*=UTF-8''dev_document.xlsx
Connection: keep-alive
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
Referrer-Policy: same-origin
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Access-Control-Expose-Headers: Content-Disposition
Keep-Alive: timeout=60

image

In both cases headers seem to be exactly the same, I will do more digging here because something is not right.
Any help appreciated though :)

from spring-framework.

mariuszpala avatar mariuszpala commented on July 28, 2024

Ticket can be closed, we found the issue was on the frontend side which incorrectly parsed the Content-Disposition header.

from spring-framework.

bclozel avatar bclozel commented on July 28, 2024

Thanks for letting us know

from spring-framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.