Im generating a xls file using the gem axlsx
and in Rails I use send_data method to send back the file as response but there is no related method on jets.
How to send back files?
Thanks!
Im generating a xls file using the gem axlsx
and in Rails I use send_data method to send back the file as response but there is no related method on jets.
How to send back files?
Thanks!
Jets currently does not support send_data
method. Will consider a PR for this. However, this may be useful to know. There are some issues with sending binary data on API Gateway that limits the usefulness of send_data
.
When I was playing with sending binary data via send_data
, had some issues with things like images. Here’s a post with some details on this issue https://forums.aws.amazon.com/thread.jspa?threadID=243584
Essentially, some binary files do not get served correctly unless the client sends specific Accept headers. Examples:
Does not work:
curl URL -H 'Accept: image/webp,image/*, **/** ;q=0.8' https://api-gateway-dns/image.png # responds with base64 text while
Works:
curl URL -H 'Accept: image/webp' https://api-gateway-dns/image.png # responds with image correclty
This is important because with web browsers we don’t control the Accept header when the url is being hit directly. IE: An HTML img
tag. So they don’t get served correctly.
At that point, I paused work on send_data
and ended up serving binary data or assets out of s3 instead.
This is also a reason why files in the public
folder are automatically uploaded to s3 and jets serves them out of s3 as part of the jets deploy
process: Jets Assets Serving
Thanks,
I changed my approach to send data to S3.
Thanks for your help!
Sorry to resurrect an old discussion, but I came across this while trying to figure out how to stream an image directly from a controller.
There is still no implementation of send_data
but I was able to get an image to display in the browser with the following incantation:
render(body: @image.write_to_buffer(".jpg"), content_type: "image/jpeg", "isBase64Encoded": true, headers: {'Content-Disposition': "inline", 'Content-Transfer-Encoding': 'binary'} )
That and the binary media settings in application.rb
as:
config.api.binary_media_types = ['multipart/form-data','image/jpeg','image/png', '*/*']
I tried both sets of Accept Headers quoted above and they worked.
Perhaps the underlying issue with AWS has been resolved since the original post?