通过 API 使用回形针保存文件 [英] Save files using paperclip via API

查看:68
本文介绍了通过 API 使用回形针保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用回形针管理上传,通过 Fog 备份到 S3.效果很好.

I'm using paperclip to manage uploads, backed onto S3 via Fog. It works well.

我正在尝试从电子邮件中提取附件并通过回形针(使用相同的模型等)保存它们.电子邮件由外部服务解析并发布到我的应用程序,包括附件.我收到文件本身没问题,但我不知道如何使用回形针保存它.这篇文章给了我一个 ActionDispatch::Http::UploadedFile 类型的对象.

I'm trying to take attachments out of emails and save them via paperclip (using the same model etc). Email are parsed by an external service and POSTed to my app, including the attachments. I'm receiving the file itself fine, but I can't work out how to save it using paperclip. The post gives me an object of type ActionDispatch::Http::UploadedFile.

我查看了下面的内容,但这涉及创建一个新的 File 对象.我不确定这是我想要的...我该怎么做?

I took a look at the below, but this involves creating a new File object. I'm not sure this is what I want... How should I do it?

使用 Paperclip 保存文件而无需上传

推荐答案

如果您已经有了 UploadedFile,您只需设置 Paperclip 为您提供的虚拟属性即可.

If you already have the UploadedFile, you can just set the virtual attribute Paperclip gives you to that.

所以,如果你有一个 params[:file] 和一个带有 has_attached_file :file 的模型,你应该可以只做 @obj.文件=参数[:文件];@obj.save.

So, if you had a params[:file], and a model with has_attached_file :file, you should be able to just do @obj.file = params[:file]; @obj.save.

这篇关于通过 API 使用回形针保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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