简体   繁体   English

二郎氮文件下载

[英]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. 但是现在我在Nitrogen API中找不到函数来发送文件的数据块。

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. 因此,在实践中,我需要自己处理将数据块发送到http流。

Any idea, or example how to do that, what api function can be used? 任何想法,或示例如何执行,可以使用什么api函数?

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. 1)使用基础服务器的流机制(例如,制作特定于牛仔的调度表以处理该流的牛仔处理程序模块为目标)或偏航输出文件。

2) Using cowboy, there's a bit of a hack that can work in simple_bridge if you're using cowboy. 2)使用牛仔,如果您使用牛仔,则可以在simple_bridge中使用一些技巧。 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). 如果模块的main()函数返回元组: {stream, StreamFun} ,其中StreamFun是具有2的函数( fun(Socket, Transport) -Transport是牧场运输)。 Really, this is just a shortcut way that allows you to use Transport:send(socket) to send data. 确实,这只是允许您使用Transport:send(socket)发送数据的快捷方式。 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. 将其作为实际选项添加到simple_bridge和Nitrogen可能是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM