Re: [cgi-devel] force download of a file (jpg...)
Status: Beta
Brought to you by:
drrngrvy
From: Darren G. <lis...@gm...> - 2011-01-10 17:15:05
|
Hi, On 10 January 2011 16:34, <jc...@ol...> wrote: > Hello, > > The have the user download an image (versus having the browser display the > image), one usually writes a script like this: > > header("Pragma: public"); > header("Expires: 0"); > header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); > header("Content-Type: application/force-download"); > header("Content-Disposition: attachment; filename=".basename($file)); > header("Content-Description: File Transfer"); > readfile($file); > > How would that work with the cgi library? I am not sure how to write the > equivalent of the header functions. > There is a general way to stream headers: --- cgi::response resp; resp<< cgi::header("Pragma", "public"); --- There are also some specific helpers, although only a few come "built-in": --- cgi::response resp; resp << cgi::content_type("application/force-download") << cgi::content_disposition("attachment; filename=".basename($file)"); --- After setting headers, one simple option would be to stream the file into the response (which would involve copying the contents) and then send that back to the user. An more efficient approach is to read and write the file in chunks, using the Boost.Asio write() functions on request.client(). There is an example of doing this in svn trunk, which you can find here: https://svn.boost.org/svn/boost/sandbox/SOC/2007/cgi/trunk/libs/cgi/example/fcgi/file_browser/main.cpp HTH, Darren |