azure 上载的Excel Blob未正确创建

oaxa6hgo  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(141)

我试图上传到blob的Excel文件没有正确创建,尽管它已经成功上传。我正在使用UploadBlobAsync进行相同的操作。

private async Task UploadExcelToBlobAsync(string fileName, MemoryStream excelContentMemoryStream)
        {
            try
            {
                string currentDateTime = DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss");
                string fileExtension = ".xslx";
                fileName = $"{fileName}_{currentDateTime}{fileExtension}";
                var blobContainerClient = _blobService.GetBlobContainerClient(); // this is properly instatiated
                await blobContainerClient.UploadBlobAsync(fileName, excelContentMemoryStream);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

奇怪的是,同样的MemoryStream对象,我在我的代码中进一步使用来执行一些逻辑。所以我猜,问题不在于MemoryStream对象也。下面是生成的Excel的一个片段,生成的文件大小是14KB,而我上传的是20KB:

有没有人能告诉我我到底做错了什么?先谢谢你了。

  • 添加:* 以下是调用方法UploadExcelToBlobAsync的代码:
public async Task<(string, List<CustomObject>?)> UploadExcelAsync(IFormFile file)
        {
            try
            {
                System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

                using (MemoryStream ms = new MemoryStream())
                {
                    await file.CopyToAsync(ms);
                    var excelReader = ExcelReaderFactory.CreateOpenXmlReader(ms);
                    DataSet excelDataSet = excelReader.AsDataSet();

                    await UploadExcelToBlobAsync(file.FileName, ms);

                    var resp = await ValidateAndInsertExcelData(excelDataSet);

                    return resp;
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
dtcbnfnu

dtcbnfnu1#

请试试我的代码,如果下载的文件没有出现提示,我的建议是使用我的示例代码。

[HttpPost]
    [Route("UploadExcel")]
    public async Task<IActionResult> UploadExcelAsync([FromForm] IFormCollection formfile)
    {
        string sasuri = "https://br***nWTT5GnC1LpHsD7p5Fo2lHpFM1nafAHjL9Fozf%2FvDdg%3D";
        Uri container_uri = new Uri(sasuri);
        var container_client = new BlobContainerClient(container_uri);

        var file = formfile.Files.FirstOrDefault();

        string fileName = file.FileName;
        BlobClient blobClient = container_client.GetBlobClient(fileName);
        
        Stream stream = file.OpenReadStream();
        var result = await blobClient.UploadAsync(stream, true);
        stream.Close();
        return Ok(result.GetRawResponse().Status);
    }

如果使用我的示例代码,仍然会有这样的提示:

The file could be corrupted or unsafe. Unless you trust its source, don't open it.

然后请尝试使用另一台计算机作为客户端进行测试。如果问题仍然存在。这意味着Microsoft Office版本或安全性有问题。Then try this

相关问题