使用Grape API向客户端发送文件

3

我一直在做使用carrierwave进行文件上传的rails应用程序。如何使用Grape API将现有文件发送给客户端?假设文件在目录“public/uploads/datafile/1/file.txt”中。

2个回答

4
如果没有设置“api_format”,则会出现UTF8字符转换问题。
if file_path && File.exists?(file_path)
  data = File.open(file_path, 'rb').read

  header "Content-Disposition:", " infile; filename=\"#{File.basename(file_path)}\""
  content_type 'application/x-gzip'

  env['api.format'] = :binary

  present data
end

这是在葡萄0.4.1上的版本。

1
只要您设置了内容类型,就像发送文件内容一样容易。
content_type "text/plain"
File.read(...)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接