Excel文件存储在azure blob容器中。它们在IE中没有任何意外下载,但在Chrome中页面显示此消息(并在Canary中崩溃):
This file appears corrupt
并提供了一个下载它的链接,从那时起一切都很顺利。我尝试将内容类型设置为不同的excel格式,但结果是一样的。
这是blob代码:
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);
答案 0 :(得分:1)
您错过了blockBlob.SetProperties();
试试这个:
var blockBlob = container.GetBlockBlobReference(randomFileName);
blockBlob.DeleteIfExists();
blockBlob.Properties.ContentType =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
blockBlob.SetProperties(); // This is for commiting changes.
请注意,对于.xls
个文件,您需要将content-type
设置为application/vnd.ms-excel
。
仅供参考:如果要更新现有blob中的属性值,则需要获取当前值,设置我们要更新的属性并在BLOB上调用SetProperties
。
示例:
blob.FetchAttributes();
blob.Properties.ContentType = "image/png";
blob.SetProperties();