0

我希望用户能够上传文件,然后我希望能够解析它们,取出信息片段,然后将它们声明为全局变量,供我的 Web 应用程序的其他部分使用。我知道您可以轻松地放入文件上传表单,但是我将在哪里存储用于解析文件的脚本?它会在模型​​、视图、控制器或其他地方吗?另外,我如何告诉我的应用程序在文件上传后立即运行此脚本。我会把它放在表单的 <% end %> 标记之前的视图中吗?当它解析文件时,如何确保全局声明变量(可能是数组),以便我可以在应用程序的所有其他部分调用这些变量

4

1 回答 1

0

使用EventMachine,您可以查看文件夹中的文件操作,然后对其进行处理。
图书馆rb-inotify也适合。

# Create the notifier
notifier = INotify::Notifier.new

# Run this callback whenever the file path/to/foo.txt is read
notifier.watch("path/to/foo.txt", :access) do
  puts "Foo.txt was accessed!"
end

# Watch for any file in the directory being deleted
# or moved out of the directory.
notifier.watch("path/to/directory", :delete, :moved_from) do |event|
  # The #name field of the event object contains the name of the affected file
  puts "#{event.name} is no longer in the directory!"
end

# Nothing happens until you run the notifier!
notifier.run
于 2012-12-23T00:57:31.767 回答