如何在 Rails 中保留请求的临时文件?

发布于 2024-10-03 06:39:48 字数 814 浏览 8 评论 0原文

在我的 Rails 应用程序中,用户可以上传 Excel 文件。在我的模型中,有一个类 ImportFile 使用 Attachment_fu,如下所示:

class ImportFile < ActiveRecord::Base
  has_attachment :storage => :file_system, :path_prefix => 'public/imports', :max_size => 10.megabytes
end

当用户单击“添加文件”时,他进入一个带有 <%= fields.file_field :uploaded_data %> 的页面。 Attachment_fu 完成它的工作并且文件上传正在完成(让我们忽略验证问题)。我想保留这个文件以供将来使用,因此我将上传的文件复制到其他临时文件。临时文件工作正常 - 我可以在磁盘上看到它。

def self.write_to_tmp(data)
    temp_file = Tempfile.new("import", "#{Rails.root}/tmp")
  begin
    temp_file.write(data)
  ensure
    temp_file.close(false)
  end
  temp_file
end

我想要做的是向用户显示预览,然后让他选择是否要添加文件或放弃它 - 有两个按钮。当用户选择保存文件时,我遇到问题,因为我上面刚刚创建的临时文件消失了。它在请求之前被删除。

有谁有提示如何实现这一目标?或者可以向我指出上传预览文件的场景,就像我所展示的那样?我已经找了好几天了,但一直没找到。

In my Rails application users can upload Excel files. In my model there is a class ImportFile that uses attachment_fu like this:

class ImportFile < ActiveRecord::Base
  has_attachment :storage => :file_system, :path_prefix => 'public/imports', :max_size => 10.megabytes
end

When user clicks "Add file" he enters a page with a <%= fields.file_field :uploaded_data %>. attachment_fu does it's job and file upload is being done (let's ommit validation problems). I want to keep this file for future so I copy uploaded file to other temp file. Temp file is working fine - I can see it on disk.

def self.write_to_tmp(data)
    temp_file = Tempfile.new("import", "#{Rails.root}/tmp")
  begin
    temp_file.write(data)
  ensure
    temp_file.close(false)
  end
  temp_file
end

What I want to do is to show user a preview and then let him choose if he wants to add a file or discard it - there are two buttons. I have a problem when user chooses to save a file, because a temp file I've just created above is gone. It is deleted before request.

Does anyone has hints how to achive this? Or can point me to upload-with-preview file scenario like the one I've presened? I've been looking for days, but I've failed to find one.

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

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

发布评论

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

评论(1

逆光飞翔i 2024-10-10 06:39:48

处理此类事情最可靠的方法是创建一个简单的“上传”跟踪模型,就像您在那里一样,但使用 改为回形针。这可以配置为处理大量文件。

您需要实际保存这些记录,以便它们在请求之间持续存在。这将导致孤立的记录,但是一个简单的 cron 作业或 rake 任务您可以在需要时随时删除所有未使用的文件。

在单个目录中创建大量文件通常是一个坏主意。 Paperclip 有一个路径参数,它将把您的 ID 号分成几部分,因此记录 #903132 会进入 .../90/31/32。

保留常规附件,如果他们想丢弃它,请将其删除,否则使用它。稍后清理所有未使用的附件。

The most reliable approach to this sort of thing would be to create a simple "upload" tracking model like you have there, but using Paperclip instead. This can be configured to handle a very large number of files.

You need to actually save these records for them to persist between requests. This will lead to orphaned records, but a simple cron job or rake task you can kill off all the unused files any time you need to.

Creating a large number of files in a single directory is usually a bad idea. Paperclip has a path parameter which will split up your ID number into parts, so record #903132 goes into .../90/31/32 for example.

Keep a regular attachment, and if they want to discard it, delete it, otherwise use it. At some point later clean out all the unused attachments.

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