铁轨 + CouchDb 二进制文件上传到数据库

发布于 2024-10-19 00:58:43 字数 997 浏览 7 评论 0原文

Simple-Stored 基于 CouchPotato 顶部,用于在 Rails 中处理 CouchDb。在尝试将文件上传到 couchdb 时,我们尝试使用 base64、json post,但似乎没有任何效果;我们正在尝试上传到已存储文档的 _attachments 属性。

拥有这样的模型:

class Patient
  include SimplyStored::Couch
end

并在控制器中通过更新操作接收文件

def update
    @patient = Patient.find(params[:id])
    if params[:patient][:_attachments] 
        attachement = params[:patient][:_attachments]
        filedata = attachement.tempfile.read
        data = Base64.encode64(filedata).gsub(/\n/, '')
        type = attachement.content_type
        or_name = attachement.original_filename
        @patient._attachments = {or_name => {'data' => data, 'content_type' => type}}
        @patient.save
        return render :json => @patient._attachments
    end 
end

现在有趣的部分是我可以看到 @ Patient._acttachments 具有文件本身,这就是 .save 之后渲染中返回的内容;但它实际上并没有将其保存在 couchdb 数据库中。

任何想法为什么不进行保存,或者我应该尝试将 _attachment 推送到 couchdb 数据库。 ? (顺便说一句,它总是返回 500 错误:( )

Simple-Stored is based on top of CouchPotato for handling CouchDb in rails. While trying to upload files to the couchdb we have tryied using base64, json post and nothing seems to work quite right; we are trying to upload to the _attachments propertie of a document already stored.

Having the model like this :

class Patient
  include SimplyStored::Couch
end

and in the controller while receiving the file trough the update action

def update
    @patient = Patient.find(params[:id])
    if params[:patient][:_attachments] 
        attachement = params[:patient][:_attachments]
        filedata = attachement.tempfile.read
        data = Base64.encode64(filedata).gsub(/\n/, '')
        type = attachement.content_type
        or_name = attachement.original_filename
        @patient._attachments = {or_name => {'data' => data, 'content_type' => type}}
        @patient.save
        return render :json => @patient._attachments
    end 
end

Now the fun part is that I can see that @patient._acttachments has the file itself and that is what is returning in the render after the .save; but it is not actually saving it on the couchdb database.

Any ideas why is not doing the save or should I try to just push the _attachment to the couchdb database. ? (which by the way always returns a 500 error :( )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2024-10-26 00:58:43

解决方案非常简单,基于 couchpotato 网站,您实际上不需要将其转换为 base64 这里是代码工作的示例,

if params[:patient][:_attachments] 
            attachement = params[:patient][:_attachments]
            data = attachement.tempfile.read
            type = attachement.content_type
            or_name = attachement.original_filename
            params[:patient][:_attachments] = {or_name => {'data' => data, 'content_type' => type}}
end 
  if @patient.update_attributes(params[:patient]) #blah blah blah

因为值位于 [:patent][:_attachments] 参数中,您只需要将其作为另一个经过测试和工作的参数传递。

此外,您还需要定义您的患者模型,

    property :_attachments

不知道是否需要,但我做到了。

我知道我不应该要钱,但因为我为四个人工作,每小时只有 100 比索..办公室见,

欢呼

,哈哈

the solution it's very simple, based on the couchpotato website, you actually don't need to convert it to base64 here is the example of code working

if params[:patient][:_attachments] 
            attachement = params[:patient][:_attachments]
            data = attachement.tempfile.read
            type = attachement.content_type
            or_name = attachement.original_filename
            params[:patient][:_attachments] = {or_name => {'data' => data, 'content_type' => type}}
end 
  if @patient.update_attributes(params[:patient]) #blah blah blah

since the values are in the [:patient][:_attachments] params, you just need to pass it as another param tested and working.

Also you need to define your patients model as

    property :_attachments

dunno if that is required but I did it.

I know I should not ask for money but since I WORK FOUR YOU its only 100 pesos/hour.. see you at the office

cheers

lols

难如初 2024-10-26 00:58:43

我不知道 Ruby 和 couchpotato,但我认为您不需要对附件进行 Base64 处理。只需读取二进制信息并将其写入请求即可。
我的2美分。 :)

I donno about the Ruby and couchpotato, but I don't think you need to Base64 your attachment. Just read the binary info and write it to request.
my 2cents. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文