如何使用paperclip-google驱动器从谷歌驱动器下载文件?

时间:2014-10-16 15:01:07

标签: ruby-on-rails ruby ruby-on-rails-4 google-drive-api

我在google驱动器上完成了文件上传,但在下载时遇到了错误:

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

提前致谢

2 个答案:

答案 0 :(得分: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

但这不是一种正确的方法。因为您直接向最终用户开放资源。

答案 1 :(得分: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
相关问题