Repsonse.Transmitfile(); 能够节省但无法打开

Mar*_*cus 8 c# asp.net response.transmitfile

我正在尝试使用发送xlsx文件

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();
Run Code Online (Sandbox Code Playgroud)

弹出IE对话框,我可以成功保存文件,然后从文件夹打开它,工作正常和花花公子.但如果我在IE对话框中单击"打开",我会收到"无法下载myFile.xlsx".我单击"重试"并打开Excel但弹出"Excel无法打开文件'myFile.xlsx',因为文件格式或文件扩展名无效..."错误.
我目前正在调试模式下从VS2010运行该站点.

有谁知道为什么它会让我保存,但不能直接打开?

编辑
Chrome只需下载它.FF尝试打开它但是给出错误The file you are trying to open, 'myFile.xlsx.xls', is in a different format than specified by the file extension...我可以选择打开它并且它成功打开,但是在只读模式下.
所以,这里有一些时髦的东西.
fileName ="myFile.xlsx"

编辑2
这是在IE 9中.我也尝试过octet-streamapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet作为ContentType.

Pra*_*enu 5

这是因为您的ContentType错误.使用

Response.ContentType = "application/vnd.ms-excel";
Run Code Online (Sandbox Code Playgroud)

编辑

如果它不起作用,你能尝试吗?

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();
Run Code Online (Sandbox Code Playgroud)