简体   繁体   English

Rails-给定文件格式,如何存储在数据库中,然后再阅读?

[英]Rails - Given a File Form Posted, How to Store in the DB and then Read later?

Given a Rails Form Post with X number of files. 给定一个带有X个文件的Rails Form Post。

  • How do you store the file in the Database? 您如何将文件存储在数据库中?

Then, later, how do you then read the file? 然后,以后如何读取文件?

Thanks. 谢谢。

A little more background. 多一点背景。 The file form post is from SendGrid's parse api. 文件格式帖子来自SendGrid的解析API。 Then later I want to be able to read the file with delayed_job and then use paperclip to store the file and process it on S3. 然后,稍后我希望能够使用delay_job读取文件,然后使用回形针存储文件并在S3上进行处理。

Thanks 谢谢

可以肯定的是,我以相同的方式回答了您的其他问题: https : //github.com/jstorimer/delayed_pa​​perclip完成了您似乎希望它执行的操作。

Based on the limited information I know about your system, I'm assuming you've got some model that tracks the emails. 根据我对您的系统了解的有限信息,我假设您有某种跟踪电子邮件的模型。 That's a good start. 这是一个好的开始。

First I would like to clarify that in no situation should you be storing files in the database . 首先,我想澄清一下,无论如何都不要将文件存储在数据库中 Files go in the filesystem, where they can be read and written much faster. 文件进入文件系统,可以在其中更快地读取和写入文件。

Now with that in mind, I would have a model that associates with your email model, possibly called Part . 现在考虑到这一点,我将有一个与您的电子邮件模型相关联的模型,可能称为Part This model's purpose will be to use Paperclip to store the files. 该模型的目的是使用Paperclip来存储文件。 I would call the attachment part also, and so to create a new one you would do this: 我也将其称为附件part ,因此要创建一个新的附件,您可以这样做:

email.parts.build(:part => some_file)

In that case, Paperclip will take care of moving the file to where it needs to be. 在这种情况下,回形针将负责将文件移动到需要的位置。 To read the file later, Paperclip has methods for that. 要在以后读取文件,Paperclip提供了一些方法。 Check out Paperclip's documentation, it's pretty good for this sort of thing. 查阅Paperclip的文档,对于这种事情非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM