I am trying to create a multipart upload request from C# to upload a small file to Google Drive as per https://developers.google.com/drive/v3/web/multipart-upload
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token);
//api endpoint
var apiUri = new Uri("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart");
// read the image content
var imageBinaryContent = new ByteArrayContent(fileBytes);
imageBinaryContent.Headers.Add("Content-Type", "image/jpeg");
// prepare the metadata content
string metaContent = "{\"name\":\"myObject\"}";
byte[] byteArray = Encoding.UTF8.GetBytes(metaContent);
var metaStream = new ByteArrayContent(byteArray);
metaStream.Headers.Add("Content-Type", "application/json; charset=UTF-8");
// create the multipartformdata content, set the headers, and add the above content
var multipartContent = new MultipartFormDataContent();
multipartContent.Headers.Remove("Content-Type");
multipartContent.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundry=myboundry---");
multipartContent.Add(metaStream);
multipartContent.Add(imageBinaryContent);
HttpResponseMessage result = await client.PostAsync(apiUri, multipartContent);
}
But I can't seem to get it working. This code works fine for simple uploads to Drive:
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", access_token);
//api endpoint
var apiUri = new Uri("https://www.googleapis.com/upload/drive/v3/files?uploadType=media");
// read the image content
var imageBinaryContent = new ByteArrayContent(fileBytes);
imageBinaryContent.Headers.Add("Content-Type", "image/jpeg");
HttpResponseMessage result = await client.PostAsync(apiUri, imageBinaryContent);
}