简体   繁体   English

Google Drive API for C#-零长度文件更新不起作用

[英]Google Drive API for C# - Zero length file updates not working

I am attempting to update a Google Drive file that currently has no content. 我正在尝试更新当前没有内容的Google云端硬盘文件。 The upload body is a few bytes in length. 上载主体的长度为几个字节。 When I execute the update, the response body comes back null and the file is not updated. 当我执行更新时,响应主体返回null,并且文件未更新。

This issue also occurs when I attempt to clear the contents of a file by uploading an empty file. 当我尝试通过上载空文件清除文件内容时,也会发生此问题。

My code works for all other file updates: 我的代码适用于所有其他文件更新:

File f = service.Files.Get("myid").Execute();
FilesResource.UpdateMediaUpload r = service.Files.Update(f, f.Id, s, i.MimeType);
r.Fields = "id,md5Checksum";
r.Upload();
Console.WriteLine(r.ResponseBody.Md5Checksum);

If I do not query the response body there is no error, the file simply isn't updated. 如果我不查询响应主体,就没有错误,则文件不会更新。 I also cannot download an empty file, but that is easily worked around. 我也无法下载一个空文件,但是很容易解决。

I've had the similar issue. 我遇到过类似的问题。 The reason is that the file object, returned by the Execute method, contains a non-null property which is not directly writable . 原因是Execute方法返回的文件对象包含不可直接写的non-null属性。 In this case this property is the Id . 在这种情况下,此属性为Id By setting null to the Id the problem is solved: 通过将Id设置为null可以解决问题:

f.Id = null;

The other, more reliable solution, is to instantiate the File class and directly set the needed properties: 另一种更可靠的解决方案是实例化File类并直接设置所需的属性:

File f = new File();
f.MimeType = newMimeType;
FilesResource.UpdateMediaUpload r = service.Files.Update(f, fileId stream, newMimeType);

To check whether the update/upload operation failed you can use the following code: 要检查更新/上传操作是否失败,可以使用以下代码:

FilesResource.UpdateMediaUpload r = service.Files.Update(f, fileId, stream, newMimeType);
IUploadProgress progress =  r.Upload();
if (progress.Status == UploadStatus.Failed)
{
    if (progress.Exception != null)
    {
        throw progress.Exception;
    }
    else
    {
        throw new InvalidOperationException("upload process failed");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM