Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.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
C# 为什么';t Response.TransmitFile是否在我的WCF服务中返回我的文件?_C#_Excel_Wcf_Httpresponse_Response.transmitfile - Fatal编程技术网

C# 为什么';t Response.TransmitFile是否在我的WCF服务中返回我的文件?

C# 为什么';t Response.TransmitFile是否在我的WCF服务中返回我的文件?,c#,excel,wcf,httpresponse,response.transmitfile,C#,Excel,Wcf,Httpresponse,Response.transmitfile,若你们能解释为什么TransmitFile不工作,代码很简单,我创建excel文件(.xlsx),将其保存在服务器上并返回文件路径(通过CreateProjectsReport),然后我只想获取文件并将其传输给用户 string filePath = CreateProjectsReport(ProjectIds, items); System.IO.FileInfo file = new System.IO.FileInfo(filePath); if (file.Exists) {

若你们能解释为什么TransmitFile不工作,代码很简单,我创建excel文件(
.xlsx
),将其保存在服务器上并返回文件路径(通过CreateProjectsReport),然后我只想获取文件并将其传输给用户

string filePath = CreateProjectsReport(ProjectIds, items);

System.IO.FileInfo file = new System.IO.FileInfo(filePath);

if (file.Exists)
{
    HttpContext context = HttpContext.Current;

    try
    {
        context.Response.Clear();
        context.Response.ClearHeaders();
        context.Response.ClearContent();
        context.Response.AddHeader("Content-Disposition", 
                                   "attachment; filename=" + file.Name);
        context.Response.AddHeader("Content-Length", file.Length.ToString());

        // context.Response.ContentType = "application
        // vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        context.Response.ContentType = "application/vnd.ms-excel";
        context.Response.TransmitFile(file.FullName);
        context.Response.Flush();
        // context.Response.WriteFile(file.FullName, false);                                
    }
    catch (Exception ex)
    {
        LogException(ex);
    }
    finally
    {
        System.IO.File.Delete(filePath);
        context.Response.End();
    }
}
这段代码的结果是,文件保存在我的服务器上的预期文件夹中(完整的报告在那里,我验证了通过方法生成的excel文件),并且在
response.Flush()上,我看到文件在浏览器上传输,内容长度
30kb
,因为文件长度在服务器上,但一旦我单击“保存”并将文件保存到我的下载文件夹中,它的长度是
10kb
,用excel打开它时,我会发现错误:
excel foudn“filename.xlsx”中的不可读内容。是否要恢复…

检查IIS MIME类型,已设置(我尝试对内容类型使用这两种类型,得到相同的错误):

.xls-
应用程序/vnd.ms excel

.xlsx-
application/vnd.openxmlformats officedocument.spreadsheetml.sheet

context.Response.TransmitFile是否禁止我的文件,并且不发送该文件的全部内容?为什么会发生这种情况

编辑

通过浏览器控制台,我可以在
响应中看到。Header
属性
content length
是文件的确切长度,但一旦我单击
保存
文件,文件大小从1kb变为15kb,而不是服务器上的30kb(就像
传输文件(file.FullName)
不会传输整个文件


您使用的是Webforms吗?您不需要在任何其他堆栈中使用
context.Response
Response
。在ASP.NET MVC、Web API、Core中,您返回一个包含
文件()的文件
methods对于“filename.xlsx”中不可读内容的实际错误
Excel。是否要恢复…
它与ASP.NET或TransmitFile无关。这意味着该文件已损坏,或者根本不是Excel文件。您是如何创建该文件的?它是真实的
xlsx
文件还是带有假ex的HTML/CSV文件张力和内容类型?要创建一个真正的
xlsx
文件,请直接使用EPPlus或Open XML SDK之类的库来创建它
xls
已经过时,早在13年前的2006年就被xlsx取代了。@PanagiotisKanavos我正在使用jQuery调用我的服务,该服务创建文件并应返回响应(要下载的文件),我正在使用OfficeOpenXML(它在服务器上正确地保存和创建.xlsx文件,我确实看到了我服务器上的文件)…所以更好的问题是如何将文件从服务器返回到应用程序,我应该使用响应并将字节数组写入输出流..还是作为响应中的附件响应中的附件?响应中的附件意味着什么?HTTP并没有这个概念“附件”