Amazon S3 Bucket downloading excel file is corrupted

I uploaded and .xlsx into my Amazon S3 Bucket . The problem is when i am trying to retrieve it , the file is corrupted when i try to open it with Microsoft Excel. I am using AWS SDK for .NET.

The logic of downloading the file from the bucket :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
public async Task<Stream?> 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;
}
}
</code>
<code> public async Task<Stream?> 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; } } </code>

public async Task<Stream?> 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;
        }
    }

Than goes into my BUCKET SERVICE Layer

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
public async Task<Stream?> DownloadFile(string s3Key)
{
var getFileStream = await _bucketAcces.DownloadFile(s3Key);
return getFileStream;
}
</code>
<code> public async Task<Stream?> DownloadFile(string s3Key) { var getFileStream = await _bucketAcces.DownloadFile(s3Key); return getFileStream; } </code>

public async Task<Stream?> DownloadFile(string s3Key)
    {
        var getFileStream = await _bucketAcces.DownloadFile(s3Key);

        return getFileStream;

    }

Than into my ADMIN SERVICE LAYER:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> public Task<Stream?> GetExcelFile()
{
return _bucketService.DownloadFile("produseExcelExample.xlsx");
}
</code>
<code> public Task<Stream?> GetExcelFile() { return _bucketService.DownloadFile("produseExcelExample.xlsx"); } </code>
 public Task<Stream?> GetExcelFile()
    {
        return _bucketService.DownloadFile("produseExcelExample.xlsx");
    }

Than into my controller:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public async Task<IActionResult> 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");
}
</code>
<code>public async Task<IActionResult> 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"); } </code>
public async Task<IActionResult> 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");
        }

The file gets downloaded , everything is fine , but when i try to open it i get the corrupted file error:

And then

UPDATE

This is how i receive it on the frontend(forgot to mention this):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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);
}
}
</code>
<code>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); } } </code>
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);
    }
}

I’m, using Vue.js 3 for the frontend

UPDATE 2

It has 10.77kB and the file i am downloading has 16.77kB. So something is not good when i retrieve it from the bucket, that s why it might be corrupted.

What to do here ?

UPDATE SOLVED

Such a rookie mistake i made. Of course the file was treated as JSON not Blob. Forgot to put blob in my responseType on the API request to the backend.

Recognized by AWS

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật