Excel file (azure blob) does not download in chrome

1k views Asked by At

Excel files are stored in azure blob containers. They are downloaded without incident in IE but in Chrome the page displays this message (and in Canary it crashes):

This file appears corrupt

and provides a link to download it and all is well from that point. I've tried setting the content-type to different excel formats but the result is the same.

Here's the blob code:

  MemoryStream memoryStream = new MemoryStream();
  CreateFile(memoryStream, grid);
  memoryStream.Position = 0;
  var blockBlob = container.GetBlockBlobReference(randomFileName);
  blockBlob.Properties.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  blockBlob.DeleteIfExists();

  var options = new BlobRequestOptions()
  {
      ServerTimeout = TimeSpan.FromMinutes(10)
   };

   try
   {
            blockBlob.UploadFromStream(memoryStream, null, options);
   }
   catch (Exception e)
   {
       _logger.Error("Uploading excel file: Error: {0}",  e.Message);
   }


   return new Uri("https://myblobs.blob.core.windows.net/" + "containername/" + randomFileName);
1

There are 1 answers

1
bot_insane On BEST ANSWER

You missed blockBlob.SetProperties();

Try this:

   var blockBlob = container.GetBlockBlobReference(randomFileName);
   blockBlob.DeleteIfExists();
   blockBlob.Properties.ContentType = 
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
   blockBlob.SetProperties(); // This is for commiting changes.

Note that for .xls files you need to set content-type to application/vnd.ms-excel.

FYI: If you want to update property values in existing blob you need to fetch the current values, set the property that we want to update and call SetProperties on the BLOB.

Example:

    blob.FetchAttributes();
    blob.Properties.ContentType = "image/png";
    blob.SetProperties();