首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可以使用URL强制下载吗?文件不在我的Rails中

可以使用URL强制下载吗?文件不在我的Rails中
EN

Stack Overflow用户
提问于 2020-06-11 04:28:32
回答 1查看 178关注 0票数 0

我正在尝试创建播客页面。

我有mp3文件URL https://mcdn.podbean.com/mf/web/tcips9/Introverted.mp3

我想要有一个链接到mp3文件网址的下载按钮,所以当我点击它时,我想下载,而不是打开一个新的web浏览器并播放。

代码语言:javascript
复制
= link_to "Download", @podcast.episode_audio_url, download: "{@podcast.episode_audio_url}"

我尝试了上面的代码,它打开了一个新的web浏览器并播放。

我如何实现我的目标?请帮帮我。

我的控制器

代码语言:javascript
复制
    class PodcastsController < ApplicationController
  before_action :set_podcast, only: [:show, :edit, :update, :destroy]

  # GET /podcasts
  # GET /podcasts.json
  def index
    @podcasts = Podcast.all
  end

  # GET /podcasts/1
  # GET /podcasts/1.json
  def show
  end

  # GET /podcasts/new
  def new
    @podcast = Podcast.new
  end

  # GET /podcasts/1/edit
  def edit
  end

  # POST /podcasts
  # POST /podcasts.json
  def create
    @podcast = Podcast.new(podcast_params)

    respond_to do |format|
      if @podcast.save
        format.html { redirect_to @podcast, notice: 'Podcast was successfully created.' }
        format.json { render :show, status: :created, location: @podcast }
      else
        format.html { render :new }
        format.json { render json: @podcast.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /podcasts/1
  # PATCH/PUT /podcasts/1.json
  def update
    respond_to do |format|
      if @podcast.update(podcast_params)
        format.html { redirect_to @podcast, notice: 'Podcast was successfully updated.' }
        format.json { render :show, status: :ok, location: @podcast }
      else
        format.html { render :edit }
        format.json { render json: @podcast.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /podcasts/1
  # DELETE /podcasts/1.json
  def destroy
    @podcast.destroy
    respond_to do |format|
      format.html { redirect_to podcasts_url, notice: 'Podcast was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_podcast
      @podcast = Podcast.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def podcast_params
      params.require(:podcast).permit(:episode_url, :episode_title, :episode_description, :episode_audio_url, :episode_number)
    end
end

提前谢谢你。

EN

回答 1

Stack Overflow用户

发布于 2020-06-11 04:49:54

我建议你在应用程序控制器中编写下载逻辑。所以在你的路线中你会有

代码语言:javascript
复制
get download_podcast, to: "downloads#podcast"

你的控制器应该是

代码语言:javascript
复制
class DownloadsController
  def podcast
    send_file Podcast.find(params[:podcast_id]).episode_audio_url, type: "audio/mp3"
  end
end

,并且您的视图将链接到此操作

代码语言:javascript
复制
link_to "Download", download_podcast_path(podcast_id: @podcast.id), target: "_blank"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62312550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档