Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 从MemoryStream返回时,ASP.net MVC ActionResult会损坏excel文件_Asp.net Mvc 3_Excel_Memorystream - Fatal编程技术网

Asp.net mvc 3 从MemoryStream返回时,ASP.net MVC ActionResult会损坏excel文件

Asp.net mvc 3 从MemoryStream返回时,ASP.net MVC ActionResult会损坏excel文件,asp.net-mvc-3,excel,memorystream,Asp.net Mvc 3,Excel,Memorystream,我的控制器中有以下操作 public ActionResult DownloadExcel() { //create and populate Excel file here C1XLBook testBook = new C1XLBook(); //populate it here MemoryStream ms = new MemoryStream();

我的控制器中有以下操作

public ActionResult DownloadExcel()
        {
            //create and populate Excel file here
            C1XLBook testBook = new C1XLBook(); 
            //populate it here

            MemoryStream ms = new MemoryStream();
            testBook.Save(ms, FileFormat.Biff8);

            return File(ms, "application/ms-excel", "test-file.xls");           
        }
打开文件时,我收到Excel消息,说文件与扩展名不匹配,文件打开时已损坏

如果我将文件保存在硬盘上并从那里返回,则一切正常:

return base.File(@"C:\LOGS\test-file.xls", "application/ms-excel", "test-excel.xls");
我最初认为Save函数在将其保存到MemoryStream时会破坏它,因此我保存并重新加载了它,并将其返回给用户是很好的-当保存在硬盘上并从那里返回时,而不是从MemoryStream返回时

有什么想法吗?我不太喜欢将文件保存在硬盘上……此外,我应该能够将其保存到MemoryStream并从那里返回


我有一个预感,可能不应该使用MemoryStream在MVC中返回文件,因为每个请求都是孤立的?

在将内存流传递给结果之前,可能需要倒带内存流(设置
ms.Position=0;
)?调用
Save
后,其位置将在末尾,而不是开头