问题
I'm having to implement a proxy on a Rails 3.1 app to overcome some cross-domain JS issues.
So far I have it retrieving web page text source seemingly right, however it commonly misses images (perhaps relative paths?) in the page and then when I direct it with an absolute path to an image it will show the ascii encoding of the image rather than the image itself, I think for obvious reasons from the code for someone familiar with the topic.
I was hopeful that someone would be able to revise the following code so as to work also properly with an image proxy situation:
proxy_controller.rb:
class ProxyController < ApplicationController
def get
url = URI.parse(params["url"])
result = Net::HTTP.get_response(url)
render :text => result.body
end
end
routes.rb:
get "proxy" => "proxy#get", :as => "proxy"
Calling it via:
http://<my_dev_server>/proxy?url=http://<somedomain.tld>/path/to/page/images/image.jpg
or
http://<my_dev_server>/proxy?url=http://<somedomain.tld>/path/to/page
回答1:
instead of render, use send_data
回答2:
i think that you should use base64 encode if you will show this get on html
send_data Base64.encode64(result.body), type: result.content_type, disposition: 'inline'
and in your html and javascript request using
$('#image-wrapper').html('<img src="data:image/png;base64,' + data + '" />')
来源:https://stackoverflow.com/questions/8891161/rails-proxy-controller-not-pulling-images-through-properly-how-to-modify-approp