简体   繁体   English

Ruby on Rails使用send_file发送图像并使用javascript请求文件

[英]Ruby on rails send image with send_file and request the file in javascript

I have to retrieve an image from an existing ruby on rails backend with Javascript and show it in html. 我必须使用Javascript从Rails后端的现有红宝石中检索图像,并以html显示。

The file is send in Image_controller with the following line: 该文件通过以下行在Image_controller中发送:

send_file(image.path(style), type: image.content_type, disposition: 'inline') send_file(image.path(style),type:image.content_type,处置:'inline')

How can I retrieve this file in javascript over the Internet an show it in my html page? 如何在Internet上的javascript中检索此文件并将其显示在html页面中?

When I create a jquery request and send it to the ruby backend, I get a response in the succes function, but I don't know the format of the response data and how to show it in my html page. 当我创建一个jquery请求并将其发送到ruby后端时,我在succes函数中得到一个响应,但是我不知道响应数据的格式以及如何在html页面中显示它。

All you really need to do is respond with the image url instead of sending the file. 您真正需要做的就是用图像URL进行响应,而不是发送文件。 Let javascript load the image: 让javascript加载图像:

 render json: { image_path: image.path(style) }

// javascript // JavaScript

 $.get('/myimagepath.json').done(function (res) {
   $('#target').html('<img src="' + res.image_path + '" />');
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM