I've been using the following method to upload images to Google Drive:
public string AddFile(string path, string contentType, string driveId)
{
FilesResource.CreateMediaUpload request;
using (FileStream stream = new FileStream(path,
FileMode.Open))
{
Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File
{
Name = Path.GetFileName(path),
DriveId = driveId
};
request = _service.Files.Create(
fileMetadata, stream, contentType);
request.Fields = "id";
request.SupportsTeamDrives = true;
}
IUploadProgress requestResult = request.Upload();
if (requestResult.Exception != null) throw requestResult.Exception;
Google.Apis.Drive.v3.Data.File file = request.ResponseBody;
return file == null ? "" : file.Id;
}
It was working fine until today, using the API client library version 1.40.3.1694. Now I get this sort of error in requestResult.Exception:
System.FormatException: The format of value 'bytes 0--1/660915' is invalid.
The file I'm uploading is just a small GIF test file that gets deleted right away in this case.
Am I missing something obvious?
I understand:
MemoryStream fixes itI don't yet understand:
The problem is that you're opening a FileStream, creating a request but not executing it, disposing of the FileStream and then executing the request. In your MemoryStream version, you don't dispose of the stream until the upload is complete.
Creating the initial request doesn't actually read the data from the stream - it just prepares things. You need to keep your stream open until Upload has completed. So all you need to do is make the using statement bigger:
public string AddFile(string path, string contentType, string driveId)
{
using (FileStream stream = new FileStream(path, FileMode.Open))
{
Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File
{
Name = Path.GetFileName(path),
DriveId = driveId
};
var request = _service.Files.Create(fileMetadata, stream, contentType);
request.Fields = "id";
request.SupportsTeamDrives = true;
IUploadProgress requestResult = request.Upload();
if (requestResult.Exception != null) throw requestResult.Exception;
Google.Apis.Drive.v3.Data.File file = request.ResponseBody;
return file == null ? "" : file.Id;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With