【问题标题】:Downloading file in Rails with error handling在带有错误处理的 Rails 中下载文件
【发布时间】:2019-07-07 22:05:24
【问题描述】:

我需要从 URL 获取和下载文件。用户应输入此 URL,系统应下载文件。我需要一个可以处理错误的标准做法。

这是我现在的代码:

@dl = IO.copy_stream( open( params[:url] ), 'file.txt' )
if( @dl )
    render json: {:status => true, :message => 'downloaded.'}
else
    render json: {:status => true, :message => 'downloaded.'}
end

什么是它的最佳实践以及如何处理错误?我要正常下载的文件是100KB左右。

【问题讨论】:

    标签: ruby-on-rails file download


    【解决方案1】:

    你需要在那里做的一些事情:

    1/ 使用强参数

    2/ 您在用户输入中使用 Kernel#open 存在巨大的安全风险。不要那样做。

    3/ 很确定你想要ActiveStorage 之类的东西

    【讨论】:

      猜你喜欢
      • 2015-05-05
      • 2020-09-06
      • 2018-05-14
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多