Goal: Transmit a .xlsx file from server to client using aspx response output.
Approach Tried: I used same response header for all of these approaches.
Response.TransmitFile(fileName);
Response.Output.Write(System.Text.Encoding.Default.GetString(File.ReadAllBytes(path)));
//Tried with different encodings.
Response.Output.Write(File.ReadAllText(path,
System.Text.Encoding.Default)); //Tried with different encodings.
Result: Approach 1 worked (due to some infrastructure related constraints we cannot use this approach) . But approach 2 and 3 is not working. It's resulting a corrupted .xlsx file at client. Though .xlsx is collection of xml files and a binary format, I tried to open the file using notepad++ in both server and client. I noticed that they are little different.
Question:
Please try the following:
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=myfile.xlsx");
Response.BinaryWrite(File.ReadAllBytes(path));