Generate an image an show it in a html page without write it on the disk
In a my project in Ruby On Rails (but this is not important), when I load a html page, I need to decrypt an image (jpg) and show it in the web page: after the page request, the image is decrypted an the file is write on the server disk, so the browser can show the image.
I don't want to write the image on the server, but I want to encrypt the image on the fly !
I think that ajax (jquery) could be helpfull, but I don't know how use it开发者_如何学JAVA to the image on the fly (without write the image on the disk).
Do you have any Idea ?
thank you, Alessandro
You can use send_data
instead of rendering a page. Take a look here. With this you can generate your file and send it without saveing to disk. However normal page won't be rendered. But you can use it like this:
1.Create normal html view, on example for index action:
# controller
def index
@some_resources = SomeResources.all # just whatever you need
end
# somewhere in index.html.erb view
<img src="rendered_images/my_rendered_image.jpg" />
2.Add route for rendered_images
# routes
match "rendered_images/:filename" => "application#render_image"
3.Add render image action in application controller
# application controller
def render_image
send_data my_function_to_generate_file, :filename => params[:filename], :type => 'image/jpeg', :disposition => :inline
end
I haven't tested it. I hope it will give you something to start with!
The only way to do this is the data URI scheme.
It allows you to embed the image data directly as the src
property of the <img>
element .
However, it has its limitations: It doesn't work at all in IE < 8, and has a 32k size limit in IE 8.
- MDC on data URIs for Mozilla
MSDN Article on Data URIs for IE
Here is an article that claims to provide a workaround for IE < 8 (haven't tested)
精彩评论