首先使用 rebar 将 riak-erlang-client 作为插件添加到 Nitrogen。在项目中的 rebar.config 中添加如下依赖项。芭蕉资源
{deps, [
{riakc, "1.4.1",
{git, "git://github.com/basho/riak-erlang-client",
{tag, "1.4.1"}}}
]}.
然后在您的应用程序中运行 make
cd ../../myapp
make
使用例如这个Nitrogen 上传示例上传文件。如果在此处找到源代码。
在 finish_upload_event 部分中,捕获暂存文件夹中的 LocalFileName 文件路径。使用文件路径读取文件。
event(_) -> ok.
start_upload_event(Tag) ->
wf:flash(wf:f("Upload started with tag (~p)", [Tag])).
finish_upload_event(_Tag, undefined, _, _) ->
wf:flash("Please select a file."), ok;
finish_upload_event(_Tag, _FileName, LocalFileName, _Node) ->
{ok, Binary_image} = file:read_file(LocalFileName),
%% Open a connection to the Riak database
{ok, Pid} = riakc_pb_socket:start(DBNode,PORT,{connect_timeout,TIMEOUT},auto_reconnect, false}])).
%% If the bucket where you save your images is called my_images, create the riak object
Obj = riakc_obj:new(term_to_binary(my_images),term_to_binary(My_key),Binary_image),
%% Save to the database
ok = riakc_pb_socket:put(Pid, Obj,[]).
从数据库中读取图像并将其显示在 Web 浏览器中
-module(image).
-include_lib("nitrogen_core/include/wf.hrl").
-compile(export_all).
main() ->
%% Set the content-type of the image
wf:content_type("image/png"),
{ok, Pid} = riakc_pb_socket:start(DBNode,PORT,{connect_timeout,TIMEOUT},auto_reconnect, false}])).
%% Read the image data from the database
{ok, Fetched} = riakc_pb_socket:get(Pid, term_to_binary(my_images),term_to_binary(My_key),[]),
%% Record the image record
Binary_image = binary_to_term(riakc_obj:get_value(Fetched)),
Binary_image.
event(_) -> ok.
在浏览器结构中,您指向图像文件的 URL,例如http://example.com/image
其中image是渲染图像文件的模块。