When requesting a client for a file, I use this code to send it:
public static Result download(String file) { File file = getRealFile(file); return Ok(file); }
But I found that the browser does not load it, but displays its contents. Answer Header:
Content-Type text/plain Transfer-Encoding chunked
What is the correct way to send a file?
Update
In response to Razvi, I found the answer seems to be good for this question: https://stackoverflow.com/a/2208778
But do we really need to set so many headers?
header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$filepath"); header("Content-Type: mime/type"); header("Content-Transfer-Encoding: binary"); // UPDATE: Add the below line to show file size during download. header('Content-Length: ' . filesize($filepath));
source share