Rails 3 - 将文件上传到公共目录 [英] Rails 3 - upload files to public directory
本文介绍了Rails 3 - 将文件上传到公共目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种将文件(xml 文件)上传到 Rails 3 的公共目录的简单方法.一旦它在那里,我想打开它,解析内容并在此之后删除文件.
I'm looking for a simple way to upload a file (an xml file) to the public directory of Rails 3. Once it's there I want to open it, parse the contents and delete the file after that.
每当我搜索文件上传时,我都会遇到 Paperclip.但我不想将文件与对象相关联.我只是想上传它.最简单的方法是什么?
Whenever I searched for file upload I encountered Paperclip. But I don't want to associate the file with an object. I just want to upload it. What's the easiest way to do this?
推荐答案
a.表格
<%= form_for :file_upload, :html => {:multipart => true} do |f| %>
<%= f.file_field :my_file %>
<%= f.submit "Upload" %>
<% end %>
b.控制器
def file_upload
require 'fileutils'
tmp = params[:file_upload][:my_file].tempfile
file = File.join("public", params[:file_upload][:my_file].original_filename)
FileUtils.cp tmp.path, file
... # YOUR PARSING JOB
FileUtils.rm file
end
但是你可以只解析临时文件,所以你不需要将它复制到公共目录,它会自动删除
But you can parse just tempfile, so you don't need to copy it to public dir and it will automatically deleted
这篇关于Rails 3 - 将文件上传到公共目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文