【问题标题】:Ruby on rails send image with send_file and request the file in javascriptRuby on rails 使用 send_file 发送图像并在 javascript 中请求文件
【发布时间】:2015-09-04 08:22:09
【问题描述】:

我必须使用 Javascript 从现有的 ruby​​ on rails 后端检索图像并将其显示在 html 中。

文件通过以下行在 Image_controller 中发送:

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

如何通过 Internet 在 javascript 中检索此文件并将其显示在我的 html 页面中?

当我创建一个 jquery 请求并将其发送到 ruby​​ 后端时,我在 succes 函数中得到响应,但我不知道响应数据的格式以及如何在我的 html 页面中显示它。

【问题讨论】:

    标签: javascript ruby-on-rails image send


    【解决方案1】:

    您真正需要做的就是使用图片网址进行响应,而不是发送文件。让javascript加载图片:

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

    // javascript

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

    【讨论】:

    • 啊,好吧。您是否在此 URL 上发帖?还是只是一个返回图片的get请求?
    • 获取带有身份验证标头的请求以在响应之前进行身份验证。
    猜你喜欢
    • 2012-07-19
    • 2014-06-29
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 2023-04-08
    相关资源
    最近更新 更多