如何创建ruby脚本来解析用户上传的文件?

时间:2022-07-18 20:47:03

I want users to be able to upload files and then I want to be able to parse them, taking out pieces of information and then declaring them as global variables to be used by other parts of my web application. I know you can easily put in a file upload form but then where would I store the script for parsing the file? Would it be under models, views, controllers, or somewhere else? Also how can I tell my application to immediately run this script upon the file upload. Would I put it in the view before the form's <% end %> tag? When it does parse the file, how can I make sure the variables (probably array's) are declared globally so that I can call those variables in all other parts of my application

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

1 个解决方案

#1


0  

With EventMachine you can watch a folder for file operations and then process them.
The Library rb-inotify does fit aswell.

使用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

#1


0  

With EventMachine you can watch a folder for file operations and then process them.
The Library rb-inotify does fit aswell.

使用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