Rails 4,资产管道导致用户可下载文件被下载两次 [英] Rails 4, asset pipeline causes user downloadable files to be downloaded twice

查看:98
本文介绍了Rails 4,资产管道导致用户可下载文件被下载两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序目录中有一个名为"uploads"的文件夹,用户可以在其中上传文件和下载文件.我不希望上载文件夹位于公共目录中,因为我想控制下载授权.

I have a folder in my app directory named "uploads" where users can upload files and download files. I don't want the uploads folder to be in the public directory because I want to control download authorization.

在我的控制器中,我有:

In my controller, I have:

send_file Rails.root.join('app', 'uploads', filename), :type => 'application/zip', :disposition => 'inline', :x_sendfile=>true

这实际上很好.问题是,当我在生产服务器上时,当我运行rake asset:precompile并具有一个Assets目录时,该文件将下载两次.首次下载文件时,浏览器的运行就像什么都没有发生(没有加载旋转)一样,但是我看到数据正在Google Chrome Web开发人员的网络"标签中传输.然后,在下载文件之后,出现提示,询问用户是否要下载文件.

This actually works fine. The problem is that when I'm on the production server, when I run the rake assets:precompile, and have an assets directory, the file downloads twice. The first time the file downloads, the browser acts as if nothing is going on (no loading spinning), but I see data being transferred in the Google Chrome web developer Network tab. Then after the file has been downloaded, a prompt comes up asking the user if he/she wants to download the file.

删除公用目录中的资产文件夹可以解决此问题,但是我想使用资产管道.我还尝试过将资产管道需求从require_tree更改为require_directory.

Removing the assets folder in the public directory gets rid of this problem, but I want to use the asset pipeline. I also tried changing the asset pipeline requires from require_tree to require_directory.

有人知道如何使send_file与资产管道正常工作吗?

Does anyone know how to get send_file working properly with the asset pipeline?

谢谢.

推荐答案

对于有此问题的任何人,我都可以解决.通过

For anyone having this problem, I solved it. Pass

'data-no-turbolink' => true 

进入link_to帮助器,以阻止Turbolinks干扰下载.

into the link_to helper to stop Turbolinks from messing with the download.

https://github.com/rails/turbolinks/issues/182

这篇关于Rails 4,资产管道导致用户可下载文件被下载两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆