Ejemplo n.º 1
0
        public async static Task <IActionResult> DownloadFile(string fileName, string downloadType, ExecutionContext context)
        {
            var container = AzureSetting.GetCloudBlobContainer(context);
            await container.CreateIfNotExistsAsync().ConfigureAwait(false);

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

            if (await blockBlob.ExistsAsync().ConfigureAwait(false))
            {
                MemoryStream memoryStream = new MemoryStream();
                await blockBlob.DownloadToStreamAsync(memoryStream).ConfigureAwait(false);

                if (downloadType == DownloadType.File.ToString())
                {
                    return(new FileContentResult(memoryStream.ToArray(), "application/octet-stream")
                    {
                        FileDownloadName = fileName
                    });
                }
                else if (downloadType == DownloadType.Base64.ToString())
                {
                    var fileContent = new { FileName = fileName, Base64 = memoryStream.ToArray() };
                    return(new OkObjectResult(fileContent));
                }
            }
            return(new BadRequestObjectResult("file not exist"));
        }
        private async static Task UploadFile(ExecutionContext context, IFormFile file, ILogger log)
        {
            log.LogInformation($"Uploading started for file:{file.FileName}");

            var container = AzureSetting.GetCloudBlobContainer(context);
            await container.CreateIfNotExistsAsync().ConfigureAwait(false);

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(file.FileName);

            using var stream = file.OpenReadStream();
            await blockBlob.UploadFromStreamAsync(stream).ConfigureAwait(false);

            log.LogInformation($"Uploading done for file:{file.FileName}");
        }