首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Google Ruby API客户端创建文件响应

Google Ruby API客户端创建文件响应
EN

Stack Overflow用户
提问于 2016-08-02 21:27:23
回答 1查看 176关注 0票数 0

我目前正在做一个Rails项目,其中一个文件被上传到Drive。我可以将文件上传到驱动器,但是我想知道如何获得包含文件ID、链接等的响应。我需要使用列表吗?任何帮助都将不胜感激。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def create
  @essay = Essay.new(params.require(:essay).permit(:course_name))

  # Uploaded File
  uploaded_io = params[:essay][:essay_draft]

  # Save to a temporary folder
  Tempfile.open(uploaded_io.original_filename, Rails.root.join('private', 'tmp')) do |f|

  # Write using UTF-8 encoding
  f.write(uploaded_io.read.force_encoding("UTF-8"))

  # Close the file
  f.close

  # Gotta unlink to delete the temp file
  f.unlink
  end

  # Set Metadata to be sent to Google Drive
  file_metadata = {
  name: uploaded_io.original_filename,
  mime_type: 'application/vnd.google-apps.document'
  }

  # Call method which will upload the actual file to Drive
  @drive.create_file(file_metadata,
                  fields: 'id',
                  upload_source: uploaded_io.path,
                  content_type: 'text/doc')

if @essay.save
  redirect_to @essay
else
  render :new
end
end
EN

回答 1

Stack Overflow用户

发布于 2016-08-04 04:41:12

这就是我的create方法中的内容。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def create
@essay = Essay.new(params.require(:essay).permit(:course_name))

# Uploaded File
uploaded_io = params[:essay][:essay_draft]

# Set Metadata to be sent to Google Drive
file_metadata = {
  name: uploaded_io.original_filename,
  mime_type: 'application/vnd.google-apps.document'
}

# Call method which will upload the actual file to Drive
@file = @drive.create_file(file_metadata,
                  fields: 'id, web_view_link',
                  upload_source: uploaded_io.path,
                  content_type: 'text/doc')

if @essay.save
  render :show
else
  render :new
 end
end

然后,我可以将以下内容放入我的视图中:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<%= @file.id %>
<%= @file.web_view_link %>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38730752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文