Repsonse.Transmitfile (); Ability to save, but can not open

I am trying to send an xlsx file 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(); 

An IE dialog box will appear and I can successfully save the file and then open it from a folder that works fine and dandy. But if I click "Open" in the IE dialog box, I get "myFile.xlsx could not be loaded." I press "Retry" and open Excel, but pops up "Excel cannot open the file" myFile.xlsx "because the file format or file extension is invalid ...". I am currently starting a site from VS2010 in debug mode.

Does anyone know why this will allow me to save, but not open directly?

Edit
Chrome just downloads it. FF tried to open it, but gave 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 anyway, and It opens successfully, but in readonly mode.
So, something scared is happening here. fileName = "myFile.xlsx"

Edit 2
This is in IE 9. I also tried octet-stream and application/vnd.openxmlformats-officedocument.spreadsheetml.sheet as ContentType.

+8
source share
5 answers

This is because your ContentType is wrong. Use

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

Edit

if it does not 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(); 
+5
source share

I had the same problem once and was solved by reviewing the caching instructions sent by the application server, in this case IIS. Sometimes you add headers at the application level, which prevents the file from being saved under certain conditions, for example, in HTTPS connections. Make sure you do not enter conflicting instructions.

+2
source share

I ran into a similar problem.

It turned out that I was reading from the end of the stream, so the byte array was not populated with anything.

Setting the position of the stream 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.

Regarding "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(); 
+1
source share

Coincidentally, I'm right in the middle of something like that ...

MIME type for use in xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet , see this question .

0
source share

I was shocked by this problem, especially with longer files.

In our case, using the Content-Length header, the problem is fixed. After the content goes to a certain length, you get this problem.

Example (where tempsbldr is StringBuilder):

  Response.AddHeader("Content-Length",tempsbldr.Length.ToString()); 
0
source share

All Articles