Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 通过Sinatra上传图像_Ruby_Image_Sinatra - Fatal编程技术网

Ruby 通过Sinatra上传图像

Ruby 通过Sinatra上传图像,ruby,image,sinatra,Ruby,Image,Sinatra,我正在使用本页中的示例代码: 当我尝试上载一个图像文件(png或jpg)时,它会成功上载,并且我可以在适当的目录中看到该文件,但在上载过程中它会被损坏。我无法打开图像。在与原始文件进行比较时,我看到上传的版本中缺少几个新行 我在Windows上运行Ruby 1.9.3p392 编辑: 我试过西纳特拉的测试 File.open('57-new.jpg', "wb") do |f| f.write(File.open('57.jpg', 'rb').read) end 这很有效。唯一的区别是

我正在使用本页中的示例代码:

当我尝试上载一个图像文件(png或jpg)时,它会成功上载,并且我可以在适当的目录中看到该文件,但在上载过程中它会被损坏。我无法打开图像。在与原始文件进行比较时,我看到上传的版本中缺少几个新行

我在Windows上运行Ruby 1.9.3p392

编辑: 我试过西纳特拉的测试

File.open('57-new.jpg', "wb") do |f|
  f.write(File.open('57.jpg', 'rb').read)
end
这很有效。唯一的区别是添加了二进制标志。当使用Sinatra时,我可以在写操作上设置二进制标志,但我不确定如何在读操作上设置它,因为请求似乎向我传递了一个文件对象

File.open('uploads/' + params['myfile'][:filename], "wb") do |f|
  f.write(params['myfile'][:tempfile].read)
end

好的,看来我只需要打开新文件时的二进制标志

File.open('uploads/' + params['myfile'][:filename], "wb") do |f|
  f.write(params['myfile'][:tempfile].read)
end 

我严重怀疑该文件是否被Ruby
文件
类损坏。我愿意接受任何解释。我刚刚说了我所看到的。谢谢你最后的回答。节省了我很多时间!