อ้างอิงจาก Thai-grails-user :: ดาวน์โหลดไฟล์ใน Grails ทำไมยากจัง ?? โดยคุณสงกรานต์
เห็นว่าน่าสนใจเลยเอามาฝากครับ วิธีการ Download File ด้วย Grails ซึ่งจะทำงานใน controller class สามารถทำได้ดังนี้

class DownloadController {
	def download = {
		String imagePath = "D:\\IMG_8684.JPG"
		def file = new File( imagePath )
		response.setContentType("application/octet-stream")
		response.setHeader("Content-Disposition", "attachment;filename=${file.getName()}")
		response.outputStream << file.newInputStream()
	}
}

เท่านี้ก็สามารถทำได้แล้วครับ

Tags: , ,

Leave a Reply