gpt4 book ai didi

c# - 如何上传/转换 *.docx 到谷歌文档?谷歌云端硬盘 API v3

转载 作者:行者123 更新时间:2023-11-30 17:31:13 24 4
gpt4 key购买 nike

我在上传 microsoft/libre office 文档以通过谷歌应用进行编辑时遇到问题。对于电子表格,它工作正常,但对于文档则不然。

当我上传扩展名为 *.odt、*.docx 的文件时,我可以在 Google Drive 上通过 google viewer 查看内容,但是当我单击文档顶部的使用按钮进行编辑时,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

最佳答案

我也苦了很久。为了让Google转换成我们需要的格式,我们在meta-data中的MimeType中指定。您还需要在内容对象中指定 MimeType 中的当前文件格式。

只有PHP的例子

$fileMetadata = new \Google_Service_Drive_DriveFile([
'name' => $this->_fileName,
'parents' => array($directoryId),
'mimeType' => 'application/vnd.google-apps.document'
]);

return $this->_service()->files->create($fileMetadata, [
'data' => $document,
'mimeType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'uploadType' => 'multipart',
'fields' => 'id',
]);

关于c# - 如何上传/转换 *.docx 到谷歌文档?谷歌云端硬盘 API v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48300401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com