httperlangnitrogen

Erlang nitrogen file download


I need to provie file download feature in my nitrogen app.

In principle I need to set headers like this:

wf:header("Content-Disposition", "attachment; filename=\"" ++ Filename ++ "\""),

but now I can't find a function in Nitrogen API to send data blocks of my file.

I need to upload file in portions because they might be very large, in addition to that, the files are not available on the local storage but the binary data are obtained from other modules. So in practice I need to handle by myself sending blocks of data to the http stream.

Any idea, or example how to do that, what api function can be used?


Solution

  • The best answer I can give you is one I answered a few days ago on the Nitrogen mailing list:

    There isn't a great way to deal with this. There are two ways to deal with this:

    1) Using the underlying server's streaming mechanisms (such as making a cowboy-specific dispatch table to targets a cowboy handler module that deals with the streaming), or a yaws outfile.

    2) Using cowboy, there's a bit of a hack that can work in simple_bridge if you're using cowboy. If your module's main() function returns the tuple: {stream, StreamFun} where StreamFun is a function with arity 2 that (fun(Socket, Transport) - Transport being a ranch transport). Really, this is just a shortcut way that allows you to use Transport:send(socket) to send data. I'll admit I haven't done this before, but it should work with a little bit of tinkering.

    Adding this as an actual option to simple_bridge and Nitrogen would probably be worthwhile.