当然,文件上传是一个HTTP POST
。现在让我们首先编写一段 Erlang 代码,它的作用是HTTP/1.1 POST with Ibrowse https://github.com/cmullaparthi/ibrowse/wiki/ibrowse-API
.
%% Assumes Ibrowse application is in Code path
ensure_ibrowse()->
case whereis(ibrowse) of
undefined -> ibrowse:start();
_ -> ok
end.
post(Link,Data,Headers)->
ensure_ibrowse(),
try ibrowse:send_req(Link,Headers,post,Data) of
{ _, _, _,Result} ->
io:format("\n\tFile Uploaded. Return: ~p~n",[Result]);
EE -> {error,EE}
catch
XX:XX2 -> {error,XX,XX2}
end.
从这里开始,我们开始做 Couchdb 的事情。
-define(Link,"http://localhost:5984/DBNAME/DOCID/Penguins?rev=LATEST_REVISION").
%% File_path must be a valid file !
upload_file(Full_file_path)->
case file:read_file(Full_file_path) of
{ok,Binary} ->
post(?Link,Binary,[{"Content-Type","image/jpeg"}]);
Error -> Error
end.
就这样吧!您所需要做的就是自定义您的宏Link
适合您的沙发 DB 设置,您就可以开始了!