Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send file to a client using Grape API

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".

like image 446
Prostakov Avatar asked Jan 28 '26 20:01

Prostakov


2 Answers

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

like image 66
karlhungus Avatar answered Jan 31 '26 13:01

karlhungus


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(...)
like image 25
dB. Avatar answered Jan 31 '26 11:01

dB.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!