gpt4 book ai didi

ruby-on-rails - 如何通过表单获取临时文件的内容

转载 作者:数据小太阳 更新时间:2023-10-29 06:54:18 25 4
gpt4 key购买 nike

index.html.erb

= form_for :file_upload, :html => {:multipart => true} do |f|
= f.label :uploaded_file, 'Upload your file.'
= f.file_field :uploaded_file
= f.submit "Load new dictionary"

模型

def file_upload
file = Tempfile.new(params[:uploaded_file])
begin
@contents = file
ensure
file.close
file.unlink # deletes the temp file
end
end

索引

def index
@contents
end

但在我上传文件 = @contents

后,我的页面上没有打印任何内容

最佳答案

使用file.read读取上传文件的内容:

def file_upload
@contents = params[:uploaded_file].read
# save content somewhere
end

关于ruby-on-rails - 如何通过表单获取临时文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16648239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com