Answered by:
We found problem with some content in file while download

Question
-
User1052662409 posted
I am using s3 bucket to upload / download files using c# and .net.
Below is my code to download the file.public bool DownloadFromSubFolder(string keyName, string folderName) { var client = new AmazonS3Client(RegionEndpoint.APSouth1); string[] keySplit = keyName.Split('/'); string fileName = keySplit[keySplit.Length - 1]; string dest = Path.Combine(HttpRuntime.CodegenDir, fileName); using (client = new AmazonS3Client(RegionEndpoint.APSouth1)) { GetObjectRequest request = new GetObjectRequest(); request.BucketName = "mybucket/"+folderName; request.Key = fileName; using (GetObjectResponse response = client.GetObject(request)) { response.WriteResponseStreamToFile(dest, false); } HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + fileName); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.TransmitFile(dest); HttpContext.Current.Response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest(); //HttpContext.Current.Response.End(); // Clean up temporary file. System.IO.File.Delete(dest); return true; } }
It's working for almost all files like , .docx, .pdf etc.
But when it comes to excel files after downloading when I try to open excel it doesn't open the file.
It shows message that "We found a problem with some content in '...xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click yes."
Please suggest.Thursday, December 26, 2019 7:03 AM
Answers
-
User1052662409 posted
Lastly, there can be a problem with the original Excel file. This is especially true if the file was programmatically generated. Have you verified the Excel file does not have issues? What troubleshooting steps have you performed?I just added one line to my existing code.
HttpContext.Current.Response.SuppressContent = true;
Now it is working fine.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 2, 2020 9:36 AM
All replies
-
User475983607 posted
There are too many unknowns and potential issues with code shown to provide an accurate solution. The HTTP response does not have a content-length header. The client has no idea how many bytes to expect. You are depending on the client to figure out when the response stream ends. This is problematic with larger files that require several buffer reads.
I'm not familiar with the S3 download client, often when working with streams over HTTP/TCP. like above, you need to make sure the entire stream is received before writing to a file. Have you verified the entire file was written to disk?
using (GetObjectResponse response = client.GetObject(request)) { response.WriteResponseStreamToFile(dest, false); }
Lastly, there can be a problem with the original Excel file. This is especially true if the file was programmatically generated. Have you verified the Excel file does not have issues? What troubleshooting steps have you performed?
Thursday, December 26, 2019 2:27 PM -
User665608656 posted
Hi demoninside9,
But when it comes to excel files after downloading when I try to open excel it doesn't open the file.
It shows message that "We found a problem with some content in '...xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click yes."In my opinion, this error message usually comes from excel itself and not the code.
I suggest you can troubleshoot based on your version of excel.
There are many solutions to this issue on the Internet, and you can follow the prompts:
We found a problem with some content in filename.xlsx error in Excel
File is corrupt and cannot be opened in Excel 2016
Best Regards,
YongQing.
Friday, December 27, 2019 3:24 AM -
User1052662409 posted
Have you verified the Excel file does not have issues?There is no issue with the original excel and it it not generated by code. It is made manually by typing the text / data in the excel file.
you need to make sure the entire stream is received before writing to a file. Have you verified the entire file was written to disk?But yes, I did not notice the size difference before uploading and after downloading. There is a difference see the images below.
Before uploading
After downloading
Yes am also adding date to the file name like before uploading the name is xyz.xlsx and after uploading it is 27-12-2019-xyz.xlsx.
Please suggest.
Friday, December 27, 2019 4:34 AM -
User1052662409 posted
What troubleshooting steps have you performed?I tried with a break point, and tried to download. The file downloaded and when I open , it shows the file data with the following message box.
I think the is not downloading properly.
Monday, December 30, 2019 11:25 AM -
User1052662409 posted
Lastly, there can be a problem with the original Excel file. This is especially true if the file was programmatically generated. Have you verified the Excel file does not have issues? What troubleshooting steps have you performed?I just added one line to my existing code.
HttpContext.Current.Response.SuppressContent = true;
Now it is working fine.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 2, 2020 9:36 AM -
User475983607 posted
Now it is working fine.Are you sure? SuppressContent() returns only the HTTP header not the content.
Thursday, January 2, 2020 12:12 PM