【发布时间】:2017-03-17 18:26:56
【问题描述】:
我在我的应用程序中加入了导入功能。在开发它时,我已经给出了它在导入时检查特定文件的路径。但我不明白部署在网络上时它会如何工作。每个用户都有一个不同的文件,他们应该能够上传。那么它会考虑什么路径呢? 考虑以下代码:
#This is the view file from where i upload the CSV.
<h4>Import/Export (from/to CSV format)</h4>
<input type="file" name="csv" />
<br />
<input type="submit" name="import" value="Import from CSV"/>
<br />
<input type="submit" name="export" value="Export questions to CSV" >
以下是处理它的控制器:
def import
questionnaire_id = (params[:id])
begin
file_data = File.read(Rails.root.join('spec/features/import_export_csv_oss/'+params[:csv]))
a = QuestionnaireHelper.get_questions_from_csv(file_data,params[:id])
redirect_to edit_questionnaire_path(questionnaire_id.to_sym), notice: "All questions have been successfully imported!"
rescue
redirect_to edit_questionnaire_path(questionnaire_id.to_sym), notice: $ERROR_INFO
end
因此,目前,它会在 import_export_csv_oss 文件夹中检查我要上传的文件。在我的开发环境中,我可以把那个文件放在那里。但是当它部署在网络上时,当用户尝试从他的本地机器上传文件时它会起作用吗?
【问题讨论】:
-
您是否有一些示例代码可以证明您的担忧?处理这种场景的一种方法是将上传的文件存储到与您的应用程序代码相关的位置,例如,
CSV.open(Rails.root.join('uploads', 'new_file.csv'), "wb") do |csv| ... -
请查看已编辑的问题。
-
好的,我知道你在尝试什么;你目前的方法行不通。在您的控制器中,您正在读取服务器上已经存在的文件,而不是从客户端传递的文件。我建议阅读 Rails 的表单助手指南第 5.1 节(上传文件):guides.rubyonrails.org/form_helpers.html#what-gets-uploaded
-
非常感谢肖恩!
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 csv-import