如何在Rails中提示用户从Google Drive API保存下载的文件

时间:2013-11-14 19:35:59

标签: ruby-on-rails ruby google-api download

我正在使用Google Ruby API客户端下载Google云端硬盘文件(使用downloadUrl)。我能够成功下载该文件,但接下来我想提示Web用户在本地保存文件(到他选择的位置)。在下面的代码之后我该怎么做才能实现这个目标?

client = Google::APIClient.new
drive = client.discovered_api('drive', 'v2')

client.authorization.client_id = ENV["GOOGLE_CLIENT_ID"]
client.authorization.client_secret = ENV["GOOGLE_CLIENT_SECRET"]
client.authorization.redirect_uri = '...'
client.authorization.access_token = '...'

client.authorization.scope = https://www.googleapis.com/auth/drive
result = client.execute(
  :api_method => drive.files.get,
  :version => 'v2',
  :parameters => { 'fileId' => "..."}
)

download_url = result.data.downloadUrl

if download_url
  fileResult = client.execute(:uri => download_url)
else 
  #HANDLE ERRORS
  ....
end 

# WHAT TO ADD HERE TO PROMPT USER TO SAVE THE FILE LOCALLY?

1 个答案:

答案 0 :(得分:0)

我应该在我的问题中说过我想在不保存文件的情况下这样做。此外,我的部分问题是如何从下载响应中获取所需的信息。这就是我提出的,有效的方法:

    send_data fileResult.body,  
              :disposition => fileResult.response.env[:response_headers]["content-disposition"], 
              :type => fileResult.response.env[:response_headers]["content-type"]