Why does Chrome add two tabs at the end of my xlsx file and by doing so, is corrupting the files?

99封情书 提交于 2021-01-27 20:07:22

问题


I am developing an app with yii2 and I am using PhpSpreadsheet to generate (Excel) .xlsx files. When I download a generated .xlsx file with Chrome, I can not open it with Excel (2019), since Excel prompts that the file is corrupted, offers to repair it but fails.

First i suspected something wrong with the generator or the download process, but I've tested the download with Firefox, Edge and Filezilla - everything works as expected.

A comparison of the downloaded files in hex shows just one minor difference: At the end of the .xlsx file downloaded with other Browsers, I see

786d 6c2e 7265 6c73 504b 0506 0000 0000
0b00 0b00 d102 0000 671d 0000 0000 

but the Chrome-Download shows

786d 6c2e 7265 6c73 504b 0506 0000 0000
0b00 0b00 d102 0000 671d 0000 0000 0909

I have no idea where these extra 09's are coming from. Those are two tabs (09) chrome is adding and thus corrupting the file. OpenOffice's Calc is able to read it but Excel just can't open it.

So, again, my question is: Why is Chrome seemingly adding two tabs to the end of my file? And what can I do to prevent this?

Update: I have narrowed the problem down to "logged in with Chrome" (logged in into the yii-application, not Chrome!). Meaning: I can download and open the file with Firefox and Edge, logged in or not logged in and it works with Chrome when I am not logged in. I would blame the login but then this should also corrupt the file / add 2 extra 09's when I download it with Firefox or Edge...

Update 2: I have installed the new Edge Version 78.0.276.17 (Official build) beta (64-bit) that's based on the Chromium Engine: Same Problem! Logged in: File corrupt. Not logged in: File works!

Update 3: I've tested a random file.txt that i uploaded via FTP and there are 2 tabs added to the end of the file, when i download it via the yii's sendFile-Method:

public function actionTest(){
   Yii::$app->response->sendFile('test.txt')->send();
   return true;
}

If I download the file by directly linking to it, it's perfectly fine. Again it's a combination of Chromium, being logged in and the sendFile-method, it seems.


回答1:


And the solution is:

My i8n translations file .../frontend/languages/de-DE/app.php had two tabs in front of <?php, as suggested by @MichalHynčica! Removing those two tabs resulted in perfectly fine downloaded files.

Edit: In addition to this, the Chromium engine seems to ignore the content-length defined in the HTTP header and the actual content length of the downloaded file.

Thank you and good night.



来源:https://stackoverflow.com/questions/58382553/why-does-chrome-add-two-tabs-at-the-end-of-my-xlsx-file-and-by-doing-so-is-corr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!