简体   繁体   中英

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. 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.

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 . In this case this property is the Id . By setting null to the Id the problem is solved:

f.Id = null;

The other, more reliable solution, is to instantiate the File class and directly set the needed properties:

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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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