系统.网状物HttpContext.现在的响应解析字段



这是一个奇怪的。。。我在服务器上有一个CSV文件,并通过下面的代码向用户提供。通常,结果集是正确的,只是它总是省略最后一条记录的最后一个字段:

公司名称、处理日期、员工Id、姓氏、名字、客户授权、ENTERED_HRS、人工类型

Temp,2014-02-21,Contractor 0001,Dahlenburg,Eric,131137057134,5,0

Temp,2014-02-21,CONTRACTOR0001,Dahlenburg,Eric,1411310002,8,0

Temp,2014-02-21,CONTRACTOR0001,Dahlenburg,Eric,141132015,6.69,0

Temp,2014-02-21,Contractor 0001,Dahlenburg,Eric,141350001105,6

注意到最后一条记录的末尾没有",0"吗?在Visual Studio中,它运行良好,但在我的开发服务器上,它省略了最后一部分。

我验证了源CSV文件是否包含所有数据字段。我尝试了CSV文件的不同路径位置,但没有更改。无法理解为什么它在Visual Studio上工作而在服务器上不工作。

附录:我不知道这是否重要,但源文件位于服务器上的虚拟目录(而不是物理目录)上。有什么想法吗???

// Causes Save As Dialog box to appear for user.
String FileName = fileName;
String FilePath = strFilePath;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/csv";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.WriteFile(FilePath + FileName);
response.Flush();
response.Close();
//   response.End();  // Same as response.TransmitFile(), but causes an exception because it raises the EndRequest event.

经过大量的尝试和错误,我发现如果我使用内容类型:,我可以让它工作

response.ContentType = "application/csv";

不确定为什么这与text/csv不同,但它现在可以工作了。此外,您可以使用

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

同样,但它会提示用户文件的格式不正确,你想吗无论如何都要打开。

Eric

最新更新