我目前正在做一个Rails项目,其中一个文件被上传到Drive。我可以将文件上传到驱动器,但是我想知道如何获得包含文件ID、链接等的响应。我需要使用列表吗?任何帮助都将不胜感激。
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
发布于 2016-08-04 04:41:12
这就是我的create
方法中的内容。
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
然后,我可以将以下内容放入我的视图中:
<%= @file.id %>
<%= @file.web_view_link %>
https://stackoverflow.com/questions/38730752
复制相似问题