如何通过 Sinatra 上传多个文件并避免 NoMethodError - undefined method `read' for "#<File:0x0000000xxxxxx0>":String
How to upload multiple files via Sinatra and avoid NoMethodError - undefined method `read' for "#<File:0x0000000xxxxxx0>":String
我正在尝试从多文件数组上传第 n 个文件:
post '/upload2' do
puts params
#pp params
mx=params['images'].map{|f| f[:filename] }.join(";")
filename=mx.split(';')[0]
puts filename
mz= params['images'].map{|f| f[:tempfile] }.join(";")
file=mz.split(';')[0]
puts file
path = "/home/user/Descargas/sinatra_ajax-master/#{filename}"
File.open(path, 'wb') do |f|
f.write(file.read)
end
erb :index
end
我的HTML是:
<form action="/upload2" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" multiple />
<input type="submit" />
</form>
但是我的代码失败了,我不知道为什么。
当您 运行 join()
在您的临时文件上:
mz= params['images'].map{|f| f[:tempfile] }.join(";")
您正在获取一堆 File
对象并使用 to_s()
将它们强制转换为字符串。但是 File
的默认 to_s
是创建这个通常无用的东西:
"#<File:0x0000000xxxxxx0>"
这就是您收到错误消息的原因。
至于如何修复它,解决方法就是不要将文件转换为字符串。我不完全理解你为什么要使用一个数组,将它连接成一个字符串,然后立即将字符串拆分回一个数组。我不会那样做的:
post '/upload2' do
puts params
filename = params["images"][0][:filename]
puts filename
tempfile = params["images"][0][:tempfile]
puts tempfile
path = "/home/user/Descargas/sinatra_ajax-master/#{filename}"
File.open(path, 'wb') do |f|
f.write(tempfile.read)
end
erb :index
end
这是我的解决方案,感谢大家
更新
此代码用于上传多行,但问题只是代码的一部分
这是第一个代码
post '/upload2' do
puts params
#pp params
@filename = params[:images][0][:filename]
file = params[:images][0][:tempfile]
File.open("/home/user/Descargas/sinatra_ajax-master/#{@filename}", 'wb') do |f|
f.write(file.read)
end
erb :index
end
这是完整代码
post '/upload2' do
puts params['images'].map{|f| f[:filename] }.join(";")
k=params['images'].map{|f| f[:filename] }.join(";")
$param=k.chomp.split(";")
array_length= $param.length #or $param.size
array_lengthx = array_length - 1
puts "legth of $param is : #{array_length}"
i = 0
i=i-1
puts array_lengthx
puts i
while i.to_i < array_lengthx do
i =i+1
puts i
@filename = params[:images][i][:filename]
file = params[:images][i][:tempfile]
path="/home/user/Descargas/sinatra_ajax-master/#{@filename}"
File.open("/home/user/Descargas/sinatra_ajax-master/#{@filename}", 'wb') do |f|
f.write(file.read)
end
end
end
这是html代码
<form action="/upload2" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" multiple />
<input type="submit" />
</form>
我正在尝试从多文件数组上传第 n 个文件:
post '/upload2' do
puts params
#pp params
mx=params['images'].map{|f| f[:filename] }.join(";")
filename=mx.split(';')[0]
puts filename
mz= params['images'].map{|f| f[:tempfile] }.join(";")
file=mz.split(';')[0]
puts file
path = "/home/user/Descargas/sinatra_ajax-master/#{filename}"
File.open(path, 'wb') do |f|
f.write(file.read)
end
erb :index
end
我的HTML是:
<form action="/upload2" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" multiple />
<input type="submit" />
</form>
但是我的代码失败了,我不知道为什么。
当您 运行 join()
在您的临时文件上:
mz= params['images'].map{|f| f[:tempfile] }.join(";")
您正在获取一堆 File
对象并使用 to_s()
将它们强制转换为字符串。但是 File
的默认 to_s
是创建这个通常无用的东西:
"#<File:0x0000000xxxxxx0>"
这就是您收到错误消息的原因。
至于如何修复它,解决方法就是不要将文件转换为字符串。我不完全理解你为什么要使用一个数组,将它连接成一个字符串,然后立即将字符串拆分回一个数组。我不会那样做的:
post '/upload2' do
puts params
filename = params["images"][0][:filename]
puts filename
tempfile = params["images"][0][:tempfile]
puts tempfile
path = "/home/user/Descargas/sinatra_ajax-master/#{filename}"
File.open(path, 'wb') do |f|
f.write(tempfile.read)
end
erb :index
end
这是我的解决方案,感谢大家
更新
此代码用于上传多行,但问题只是代码的一部分
这是第一个代码
post '/upload2' do
puts params
#pp params
@filename = params[:images][0][:filename]
file = params[:images][0][:tempfile]
File.open("/home/user/Descargas/sinatra_ajax-master/#{@filename}", 'wb') do |f|
f.write(file.read)
end
erb :index
end
这是完整代码
post '/upload2' do
puts params['images'].map{|f| f[:filename] }.join(";")
k=params['images'].map{|f| f[:filename] }.join(";")
$param=k.chomp.split(";")
array_length= $param.length #or $param.size
array_lengthx = array_length - 1
puts "legth of $param is : #{array_length}"
i = 0
i=i-1
puts array_lengthx
puts i
while i.to_i < array_lengthx do
i =i+1
puts i
@filename = params[:images][i][:filename]
file = params[:images][i][:tempfile]
path="/home/user/Descargas/sinatra_ajax-master/#{@filename}"
File.open("/home/user/Descargas/sinatra_ajax-master/#{@filename}", 'wb') do |f|
f.write(file.read)
end
end
end
这是html代码
<form action="/upload2" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" multiple />
<input type="submit" />
</form>