3

I have been doing rails application that used carrierwave for file uploads. How can I send existing file to a client using Grape API? Let's assume that file is in directory "public/uploads/datafile/1/file.txt".

Prostakov
  • 910
  • 1
  • 12
  • 22

2 Answers2

4

I was getting utf8 character conversion if i didn't set 'api_format'

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

This was on grape 0.4.1

karlhungus
  • 61
  • 1
  • 3
1

As long as you set the content type, it's as easy as sending the contents of the file.

content_type "text/plain"
File.read(...)
dB.
  • 4,700
  • 2
  • 46
  • 51