0

I am trying to download an excel file which for which I am giving the proper path. But after downloading it when I am trying to open I get error as

excel cannot open the file because the file format or file extension is not valid. Verify that file has been corrupted...

But If I open that file directly it gets open, I don't understand what is the issue in it. Below is my code

protected void MultipleSpanLinkIdDownLoadbtn_Click(object sender, EventArgs e)
{
    try
    {
        string fileName = "multiSpanLinkeid.xlsx";

        FileInfo file = new FileInfo(System.Configuration.ConfigurationManager.AppSettings["MultipleSpanLinkFolder"].ToString() + "/" + fileName);
        if (file.Exists)
        {
            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            Response.AddHeader("Content-Type", "application/Excel");
            Response.ContentType = "application/vnd.xls";
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.WriteFile(file.FullName);
            Response.End();
        }
        else
        {
            Response.Write("This file does not exist.");
        }
    }
    catch (Exception)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "Exception", "alert('Template downloaded.');", true);
    }
}

Please suggest what is wrong in this

NOTE: The issue arises while downloading it from Chrome

Nad
  • 4,605
  • 11
  • 71
  • 160
  • Is this a real Excel file? Or a CSV/HTML file with a fake extension? Excel doesn't care about content types, only the actual file's content. `xls` is obsolete too, replaced in 2006 by `xlsx`. You can't even get Google Sheets or Microsoft 365 to open `xls` files without a paid subscription. – Panagiotis Kanavos Sep 08 '20 at 07:57
  • Open the downloaded file in a hex editor and post a screenshot of the hex editor showing the first bytes of the file – Caius Jard Sep 08 '20 at 07:58
  • If you want to generate a real Excel file on the server you can do so easily with libraries like EPPlus or NPOI. `xlsx` is a zip package with specific XML files. You'd need to use the *correct* content type too. `application/vnd.xls` is invalid – Panagiotis Kanavos Sep 08 '20 at 07:58
  • You set the content type header twice - why? – Caius Jard Sep 08 '20 at 07:59
  • @PanagiotisKanavos: yes the excel file is real with `.xlsx` extension and its a paid one. – Nad Sep 08 '20 at 08:00
  • @PanagiotisKanavos: I dont want to generate any excel, I already have an excel file which I want to download from path mentioned. – Nad Sep 08 '20 at 08:01
  • @hud that's not what I wrote - in this case though the error is perfectly understandable. You told Excel to treat this as `xls` when the format is `xlsx`, something COMPLETELY different – Panagiotis Kanavos Sep 08 '20 at 08:01
  • @PanagiotisKanavos: ohh...is that what it is causing the issue.. ? – Nad Sep 08 '20 at 08:01
  • @CaiusJard: Please help.. may be due to some incorrect code its giving error – Nad Sep 08 '20 at 08:03
  • @hud the code is rather mixed up. If you mix up extensions, Excel will complain. The content headers are wrong, both of them. *Can you open the original file ?* We can't help you with files or data that only exists on your machine. We can tell you to just google for `xlsx content type` to find the correct one though – Panagiotis Kanavos Sep 08 '20 at 08:04
  • @PanagiotisKanavos: I open the excel and its opening properly if i dont download from code. Can u help me with proper content header.. the data is also proper in excel – Nad Sep 08 '20 at 08:07
  • `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` – Panagiotis Kanavos Sep 08 '20 at 08:08
  • @PanagiotisKanavos: still getting the same error – Nad Sep 08 '20 at 08:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221138/discussion-between-hud-and-panagiotis-kanavos). – Nad Sep 08 '20 at 08:14
  • @PanagiotisKanavos: While debuggine when it comes to `Response.End();` It goes to catch block..is it due to that ? – Nad Sep 08 '20 at 08:17
  • @hud there was a specific reason why I asked for you to download the corrupted file, open it in a hex editor and post a screenshot of the start of the file, please do it. Google for HxD, install it, open the downloaded corrupt file, take a screenshot, add it. We want to look at the file content – Caius Jard Sep 08 '20 at 08:17
  • @CaiusJard: sure just give me a min – Nad Sep 08 '20 at 08:19
  • @CaiusJard: this is what it looks like https://i.stack.imgur.com/4AWxw.png – Nad Sep 08 '20 at 08:30
  • @CaiusJard: did u find any issue with my excel in editor ? – Nad Sep 08 '20 at 08:42
  • Well it aint an XLSX or XLS: XLSX are actually zip files so the first bytes of the file should be "PK" (on the ascii side). If it was an XLS the bytes (on the hex side) should be `D0 CF 11 E0` (leet speek for "docfile0") - the screenshot you posted is neither of these, so we need to figure out what it is/what corruption happened to it when it was downloaded – Caius Jard Sep 08 '20 at 13:25
  • Now show us the working file from the server, downloaded properly (so it works) and then opened in a hex editor. Retrieve the same doc from the server, open it in Excel to check it is working, then open it in HxD and show a screenshot of it – Caius Jard Sep 08 '20 at 13:25

2 Answers2

0
Please try with this content type.

Response.ContentType = "application/vnd.ms-excel";

or

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

  • still getting the same error while opening the excel – Nad Sep 08 '20 at 07:56
  • The content type only affects how the browser handles the file, not Excel – Panagiotis Kanavos Sep 08 '20 at 08:05
  • Please refer here: https://stackoverflow.com/questions/974079/setting-mime-type-for-excel-document – Athinarayanan Sep 08 '20 at 08:05
  • @AthinarayananRamar again, the content type affects only the browser. It doesn't affect how the file is saved. Excel never sees it. According to the question you linked `"application/vnd.ms-excel"` is the wrong content type too, that's for the obsolete `xls` format – Panagiotis Kanavos Sep 08 '20 at 08:06
  • What is the version of excel which is installed your machine? https://stackoverflow.com/questions/4212861/what-is-a-correct-mime-type-for-docx-pptx-etc/4212908#4212908 – Athinarayanan Sep 08 '20 at 08:16
0

Try this:

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
ggb778
  • 9
  • 3
  • Sorry i meant: Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; – ggb778 Sep 08 '20 at 08:22
  • and @hud should remove the line before that, `Response.AddHeader("Content-Type", "application/Excel");`. – Tsahi Asher Sep 08 '20 at 08:26