【发布时间】:2018-01-17 11:46:01
【问题描述】:
我在上传 microsoft/libre office 文档以通过 Google 应用程序进行编辑时遇到问题。对于电子表格,它可以正常工作,但对于文档则不行。
当我上传带有 *.odt、*.docx 等扩展名的文件时,我可以通过谷歌查看器在 Google Drive 上看到内容,但是当我点击使用文档顶部的按钮编辑时,Google Drive 会创建一个具有相同内容的新文件内容和名称,但我没有关于描述或任何能力的原始文件 ID 的信息。任何想法如何上传准备在线编辑的文档?
private async Task<string> UploadFileToGoogleDrive(string filePath, GoogleDriveFile file)
{
var fileData = new Google.Apis.Drive.v3.Data.File()
{
Name = file.Name,
MimeType = file.MimeType
};
if (!String.IsNullOrEmpty(file.ParentId))
fileData.Parents = new List<string>() { file.ParentId };
FilesResource.CreateMediaUpload request;
using (var stream = new FileStream(filePath, FileMode.Open))
{
request = _driveService.Files.Create(fileData, stream, StringEnum.GetStringValue(file.FileContent));
request.Fields = "id";
var uploadProgress = await request.UploadAsync();
if (uploadProgress.Exception != null)
throw uploadProgress.Exception;
}
return request.ResponseBody.Id;
}
文件 mime 类型 https://developers.google.com/drive/v3/web/manage-downloads
或使用“importFormats”参数。 https://developers.google.com/drive/v3/reference/about/get
【问题讨论】:
-
请编辑您的问题并包含您用于上传文件的代码。理想情况下包括stackoverflow.com/help/mcve
-
这是来自 Google Drive 文档的基本上传。
-
尝试设置 MimeType = "application/vnd.google-apps.document"
-
使用 MimeType = "application/vnd.google-apps.document" 我收到错误 400 错误请求。使用 MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" 有效,但我只能在线查看文件内容而不能编辑。
-
在 V2 中有一个命令会告诉 Google Drive 在上传时将文件转换为 google drive 文件类型,然后您可以对其进行编辑。我一直在挖掘我似乎找不到的文档。搜索名为 convert 的东西。
标签: c# google-api google-drive-api google-api-dotnet-client