新手问题 send_file 下载文件 文件名中文乱码

yuanrenkai · April 23, 2015 · Last by yuanrenkai replied at April 27, 2015 · 3158 hits
def download
  @file_name = params[:filename]
  if File.exists?("#{Rails.root}/public/files/#{@file_name}")
    send_file "#{Rails.root}/public/files/#{@file_name}" ,  disposition: 'attachment'
  else
    flash[:notice]="File not exist"
  end
end     

如题,下载 中文文件,名称会出现 求帮助

IE 下下载后文件名乱码解决

def get_file_name(file_name)
  str = request.user_agent
  user_agent = UserAgent.parse(str)
  user_agent.browser == 'Internet Explorer' ? CGI::escape(file_name) : file_name
end

params[:filename] 不做过滤的话,这里是个安全漏洞哦。

#2 楼 @rei 求问 这个地方只是执行了 file.exists 和 send_file 怎样会引起问题呢?

#3 楼 @jicheng1014 例如 :filename 内容是 ../../../config/database.yml

@rei 恩,谢谢,受教

@ibohsu 我 尝试了一下你的方法,,如果通过数据库来存储文件的相关信息的,这样做 木有问题,,但是,如果我是对数据文件直接操作的话,,还是未能解决问题

@ibohsu 谢谢,已完成,之前方法使用错误

@yuanrenkai 能解决了就好,社区需要您上传头像喔。

You need to Sign in before reply, if you don't have an account, please Sign up first.