I'm trying to send an xlsx file by using
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/vnd-ms.excel";
Response.TransmitFile(file.FullName);
Response.End();
The IE dialog pops up and I can successfully save the file, then open it from the folder, that works fine and dandy. But if I click on "Open" in the IE dialog, I get a "myFile.xlsx couldn't be downloaded." I click on "Retry" and it opens Excel but pops up the "Excel cannot open the file 'myFile.xlsx' because the file format or file extension is not valid..." error.
I'm currently running the site from VS2010 in debug mode.
Does anybody know why it would let me save, but not open directly?
Edit
Chrome just downloads it. FF tried opening it but gives the error The file you are trying to open, 'myFile.xlsx.xls', is in a different format than specified by the file extension...
I can choose to open it anyways and it successfully opens, but in readonly mode.
So, something funky is going on here.
fileName = "myFile.xlsx"
Edit 2
This is in IE 9. I've also tried octet-stream
and application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
as the ContentType.
It is because your ContentType is wrong. Use
Response.ContentType = "application/vnd.ms-excel";
Edit
if it didnt work, can you try
FileStream sourceFile = new FileStream(file.FullName, FileMode.Open);
float FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Length", getContent.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(getContent);
Response.Flush();
Response.End();
I had the same problem once, and solved by revising caching instructions sent by application server, in this case, IIS. Sometimes you add headers at application level that prevents the file from being saved in some conditions, like in HTTPS connections. Make sure that you aren't putting conflicting instructions.
I ran into similar issue.
Turned out I was reading from end of the stream so, the byte array wasn't getting filled with anything.
Setting the stream position to 0 (sourceFile.Position=0;) just before reading the stream (sourceFile.Read(getContent, 0, (int)sourceFile.Length);) fixed it for me.
See if that helps.
As for "Response.ContentType", "application/vnd.ms-excel" worked for me.
FileStream sourceFile = new FileStream(file.FullName, FileMode.Open);
float FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Position=0;
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Length", getContent.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(getContent);
Response.Flush();
Response.End();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With