1

I generate one xml and store it to some location say myproject/media/doc.xml As I'm newbie developer with erlang. What I knew is I will have to set request headers like

"Content-Disposition", "attachment; filename=\"" + Filename + "\""

But I'm not sure how to achieve this with ChicagoBoss. I followed the links:

  1. How to download files in ChicagoBoss?
  2. how to download file by erlang cowboy?

But still I did not get the clear idea about how to do that. What we tried to do is:

download_xml('GET', [])->
    Fun = fun (Socket, Transport) ->
      Transport:sendfile(Socket, "/home/laxmikant/Desktop/demo.xml")
    end,
    Req2 = cowboy_req:set_resp_body_fun( F, Req),%% Req is available here
    Req3 = cowboy_req:set_resp_header(<<"Content-Disposition">>, "GET", Req2),
    Req4 = cowboy_req:set_resp_header(<<"attachment;filename=\"demo.xml\"">>, "GET", Req3),
     Req5 = cowboy_req:set_resp_header(<<"Content-Length">>, "GET",  Req4),
     Req6 = cowboy_req:set_resp_header(<<"1024">>, "GET",  Req5),
    cowboy_req:reply(200, [
        {<<"content-type">>, <<"application/octet-stream">>}
    ], "", Req6).

But seems to be ChicagoBoss Request object doesn't match the Cowboy Req.

Community
  • 1
  • 1
trex
  • 3,848
  • 4
  • 31
  • 54
  • In the first link you provided, there is a method with storing your file in `priv/static`. It is the easiest solution, that does not require setting headers. What was your problem with that solution? – tkowal Jan 21 '15 at 10:38
  • @tkowal - What I think is, I don't know how to set the headers from first link? – trex Jan 21 '15 at 10:43
  • You don't have to do it at all. If you put your file in `priv/static/file.ext` and then point browser to: `http://yoursite/static/file.ext`, than CB will automatically set headers for you. You don't even need to write any controller code. – tkowal Jan 21 '15 at 10:59
  • @tkowal:- Cool, I did not know this feature. Its working now. Thanks. Can you please do one more help? How can I make browser to download file with a save as popup? Its saving automatically to a "Downloads" directory. I think, I might need to do something with js. – trex Jan 22 '15 at 06:41
  • 1
    This feature is browser specific. Most pictures will be simply displayed. Other files will be downloaded and depending on your browser setting, you will be prompted, where to save them. Don't break the user experience with forcing "save as" window, when users configured their browsers to use one folder :) – tkowal Jan 22 '15 at 07:30
  • @tkowal - Okay, Thank you! Got your view ;) Thanks for grt help! – trex Jan 22 '15 at 09:08
  • @tkowal - Can you please help us in too http://stackoverflow.com/questions/28111155/why-chunk-is-returning-some-code-while-reading-the-file-in-erlang-chicagoboss – trex Jan 23 '15 at 13:39

0 Answers0