Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用曲别针googledrive从google drive下载文件?_Ruby On Rails_Ruby_Ruby On Rails 4_Google Drive Api - Fatal编程技术网

Ruby on rails 如何使用曲别针googledrive从google drive下载文件?

Ruby on rails 如何使用曲别针googledrive从google drive下载文件?,ruby-on-rails,ruby,ruby-on-rails-4,google-drive-api,Ruby On Rails,Ruby,Ruby On Rails 4,Google Drive Api,我已经在google drive上上传了文件,但下载时出现错误: 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_

我已经在google drive上上传了文件,但下载时出现错误:

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

提前感谢

您应该先从应用程序中读取文件。然后让它可以下载

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

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

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

谢谢你的回复。但data=open(attachment.doc.path(:original)).read在上面一行给出了以下错误Errno::enoint:没有这样的文件或目录-original_25_Muster_Roll372-817338(Muster).pdf from/home/kedar/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/open uri.rb:36:“initialize”中只需从comsole中尝试查找文件。使用
attachment.doc.url
错误表明没有这样的文件。i、 文件丢失。正确识别并返回文件,然后使用
data=open(attachment.doc.url)。读取
instedi上传的文件,我已经检查了谷歌硬盘中存在的同名文件(original_25_Muster_Roll372-817338(Muster.pdf)。attachment.doc.url这一行给了我“无图片”错误。检查此文件是否为
original_25_Muster_Roll372-817338(muster).pdf
用于相同的记录?请检查此代码
attachment.doc.url
是否返回任何实时url。感谢Sachin。它非常有用的ans.open(attachment.doc.url(:original))使用此选项可以下载文档,但部分文档会出现以下错误。为什么?Errno::enoint:没有这样的文件或目录@rb_sysopen-没有来自/home/kedar/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/open uri.rb:36:“初始化”中的图片