如何使用ibrowse将附件上传到CouchDB中的文档? [英] How do I upload an attachment to a document in CouchDB using ibrowse?

查看:86
本文介绍了如何使用ibrowse将附件上传到CouchDB中的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用curl上传图片文件 Penguins.jpg 。例如:
C:\curl> curl -vX PUT -HContent-Type:image / jpeg http:// localhost:5984 / DBNAME / DOCID / Penguins?rev = LATEST_REVISION --data-binary @ Penguins.jpg

I have used curl to upload an image file Penguins.jpg. For example: C:\curl>curl -vX PUT -H "Content-Type: image/jpeg" http://localhost:5984/DBNAME/DOCID/Penguins?rev=LATEST_REVISION --data-binary @Penguins.jpg

它的工作...

那么,如何使用ibrowse实现同样的功能? =============================

So, how can I achieve the same using ibrowse? ===============================

推荐答案

自然地,文件上传是一个 HTTP POST 。现在让我们先写一下Erlang代码,这个代码是 HTTP / 1.1 POST with Ibrowse

Naturally, a file upload is an HTTP POST . Now lets first write piece of Erlang code which does HTTP/1.1 POST with Ibrowse .


%% 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.

从那里,让我们的Couch DB的东西。

From there, lets do our Couch DB thing.


-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.

你去!所有您需要做的是自定义您的宏链接以适应您的沙发数据库设置,你很好去!

There you go ! All you need to do is to customize your Macro Link to fit your couch DB settings and you're good to go !

这篇关于如何使用ibrowse将附件上传到CouchDB中的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆