我目前有两种方法:
private void UploadOrderToAzure(XLWorkbook workbook, string filename)
{
using (var memoryStream = new MemoryStream())
{
workbook.SaveAs(memoryStream);
_azureBlobService.UploadOrder(memoryStream, filename + ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
public void UploadOrder(Stream item, string name, string contentType)
{
CloudBlockBlob azureBlockBlob = OrderContainer.GetBlockBlobReference(name);
azureBlockBlob.Properties.ContentType = contentType;
// 3 months cache
azureBlockBlob.Properties.CacheControl = "max-age=7776000, must-revalidate";
azureBlockBlob.UploadFromStream(item);
}
第一种方法正确接收XLWorkbook,它不是空的。
最后一个方法的azureBlockBlob.UploadFromStream(item);
行也应该正常工作。
中间的某个地方出了问题,我最终得到了“损坏”(Excel告诉我它已损坏,但可能是因为它里面没有任何内容)0 B文件。
在尝试操作MemoryStream时,我是否遗漏了某些内容?
答案 0 :(得分:2)
A MemoryStream
就像老式的VHS录像带:它不会自动倒带。你已经写过 - 现在位置在结束。如果您开始阅读(UploadFromStream
),则无需阅读。你需要先回放:
workbook.SaveAs(memoryStream);
memoryStream.Position = 0;
_azureBlobService.UploadOrder(memoryStream, filename + ".xlsx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");