I am using Google APIs .Net client library to upload to Google drive though the Google Drive API using a service account. This works well when I try from Visual Studio (debugging) or even works when I deploy it on my local IIS.
But file does not get uploaded when I deploy it on my server (Microsoft Server 2012, IIS 8.5), also does not throw any exception. Here is the piece of code:
byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile);
Logger.LoggingService.LogError("GoogleHelper", "Is byteArray null :" + (byteArray == null)); // Line to check if bytearray is null, I am getting false in log.
Logger.LoggingService.LogError("GoogleHelper", "ByteArray length :" + byteArray.Length); // Getting actual length here.
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = DriveService.Files.Insert(body, stream, GetMimeType(uploadFile));
request.Upload();
return request.ResponseBody;
I am getting Null in return. Above code is inside try block and catch is logging the exception, but no exception is thrown.
I have given the full access to IIS user to this folder.
Anybody has faced same issue? Any pointer towards solution is welcome.
UPDATE
Its working for all files except the Office files. Since XLSX etc were not looking properly on google drive I had modified the MIME type like following:
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = System.IO.Path.GetFileName(uploadFile);
body.Description = description;
body.MimeType = GetMimeType(uploadFile, false);
body.Parents = new List<ParentReference>() { new ParentReference() { Id = parent } };
byte[] byteArray = System.IO.File.ReadAllBytes(uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = DriveService.Files.Insert(body, stream, GetMimeType(uploadFile));
request.ResponseReceived += request_ResponseReceived;
request.Upload();
return request.ResponseBody;
See I have called GetMimeType twice body.MimeType = GetMimeType(uploadFile, false);
and DriveService.Files.Insert(body, stream, GetMimeType(uploadFile))
so that file get uploaded on Google drive properly and her is my method GetMimeType:
private string GetMimeType(string fileName, bool ignoreExtension = true)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
if (ignoreExtension == false)
{
switch (ext)
{
case ".ppt":
case ".pptx":
mimeType = "application/vnd.google-apps.presentation";
break;
case ".xls":
case ".xlsx":
mimeType = "application/vnd.google-apps.spreadsheet";
break;
case ".doc":
case ".docx":
mimeType = "application/vnd.google-apps.document";
break;
default:
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
break;
}
}
else
{
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
}
return mimeType;
}