2014-10-16 241 views
5

我已经做了文件上传谷歌驱动器,但在下载时遇到错误:如何使用paperclip-googledrive从谷歌驱动器下载文件?

ActionController::MissingFile: Cannot read file original_22_Wages372-817339(wages).pdf 

我认为它没有得到谷歌驱动器路径。

附件型号:

has_attached_file :doc, 
    storage: :google_drive, 
    google_drive_credentials: "#{Rails.root}/config/google_drive.yml", 
    google_drive_options: { 
    public_folder_id: '0BwCp_aK6VhiaQmh5TG9Qbm0zcDQ', 
    path: proc { |style| "#{style}_#{id}_#{doc.original_filename}" } 
    } 

控制器:

attachment = Attachment.find(params[:id])  
send_file attachment.doc.path(:original), 
    filename: attachment.doc_file_name, 
    type: attachment.doc_content_type, 
    stream: 'true', 
    x_sendfile: true 

在此先感谢

+0

open(attachment.doc.url(:original))使用此功能可以下载文件,但有些文件会给出以下错误。为什么?Errno :: ENOENT:没有这样的文件或目录@ rb_sysopen - 没有图片来自/home/kedar/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/open-uri.rb:36: in'initialize' – Dinesh 2015-02-24 05:53:42

回答

1

Dinesh,尝试下面的代码它会解决你的问题。

attachment = Attachment.find(params[:id]) 
    data = attachment.doc.url(:original) 
    data = open(attachment.doc.url(:original)) 

    send_file data, filename: attachment.doc_file_name, 
     type: attachment.doc_content_type 
+0

谢谢萨钦。它非常有用的答案。 – Dinesh 2014-10-31 04:05:47

3

你应该从你的应用程序第一次读取文件。然后将其提供下载。

attachment = Attachment.find(params[:id]) 
data = open(attachment.doc.url).read 
send_file data, 
    :filename => attachment.doc_file_name, 
    type: attachment.doc_content_type, stream: 'true', 
    :x_sendfile => true 

你也可以把它像提供:

redirect_to attachment.doc.url 

但这不这样做正确的方式。因为你直接打开资源给最终用户。

+0

感谢您的回复。 。但 数据=开放(attachment.doc.path(:原创))读 上面一行给 变量Errno :: ENOENT以下错误:没有这样的文件或目录 - original_25_Muster_Roll372-817338(鼓起)从.PDF/home/kedar/.rvm/rubies/ruby​​-2.0.0-p0/lib/ruby​​/2.0.0/open-uri.rb:36:在'initialize'中 – Dinesh 2014-10-20 09:37:01

+2

只需从comsole中查找文件即可。使用'attachment.doc.url'错误说没有这样的文件。即文件丢失。然后使用'data = open(attachment.doc.url).read'而不是 – 2014-10-20 10:33:44

+0

已编辑的答案也 – 2014-10-20 10:47:09

相关问题