title: Google Drive API not uploading file from IIS
body: <p>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. </p>
  
  <p>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:</p>
  
  <pre><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;
  </code></pre>
  
  <p>I am getting Null in return. Above code is inside try block and catch is logging the exception, but no exception is thrown.</p>
  
  <p>I have given the full access to IIS user to this folder.</p>
  
  <p>Anybody has faced same issue? Any pointer towards solution is welcome.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>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:</p>
  
  <pre><code>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&lt;ParentReference&gt;() { 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; 
  </code></pre>
  
  <p>See I have called GetMimeType twice <code>body.MimeType = GetMimeType(uploadFile, false);</code> and <code>DriveService.Files.Insert(body, stream, GetMimeType(uploadFile))</code> so that file get uploaded on Google drive properly and her is my method GetMimeType:</p>
  
  <pre><code>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 &amp;&amp; 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 &amp;&amp; regKey.GetValue("Content Type") != null)
                  mimeType = regKey.GetValue("Content Type").ToString();
          }
  
  
          return mimeType;
      }
  </code></pre>