Логика загрузки файла из корзины:
Код: Выделить всё
public async Task DownloadFile(string s3Key)
{
try
{
var getFile = new GetObjectRequest
{
BucketName = _bucketName,
Key = s3Key
};
var response = await AmazonS3Client.GetObjectAsync(getFile);
return response.ResponseStream;
}
catch (AmazonS3Exception e)
{
Console.WriteLine(e);
return null;
}
}
Код: Выделить всё
public async Task DownloadFile(string s3Key)
{
var getFileStream = await _bucketAcces.DownloadFile(s3Key);
return getFileStream;
}
Код: Выделить всё
public Task GetExcelFile()
{
return _bucketService.DownloadFile("produseExcelExample.xlsx");
}
Код: Выделить всё
public async Task GetExcelFile()
{
var fileStream = await _adminService.GetExcelFile();
if (fileStream is not null)
{
var fileMemoryStream = new MemoryStream();
await fileStream.CopyToAsync(fileMemoryStream);
fileMemoryStream.Position = 0;
var fileStreamResult = new FileStreamResult(fileMemoryStream,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "Exemplu_excel.xlsx"
};
return fileStreamResult;
}
return NotFound("Error on downloading file");
}

А затем
ОБНОВЛЕНИЕ
Это как я получаю его во внешнем интерфейсе (забыл упомянуть об этом):
Код: Выделить всё
const downloadExcel = async () => {
try {
// Call the admin service to get the file
const response = await adminService.downloadExcel();
// Convert the response into a Blob (binary large object)
const blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
// Create a URL for the blob object
const url = window.URL.createObjectURL(blob);
// Create an anchor element and set the href to the blob URL
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'exemplu_fisier.xlsx'); // Set the desired file name
// Programmatically click the anchor to trigger the download
document.body.appendChild(link);
link.click();
// Clean up by removing the anchor and revoking the object URL
link.remove();
window.URL.revokeObjectURL(url);
} catch (error) {
fireAlarm(showErrorFileDwonload);
console.error("Error downloading Excel file:", error);
}
}
Что здесь делать?
Подробнее здесь: https://stackoverflow.com/questions/790 ... -corrupted
Мобильная версия