Angularjs 为什么IE忽略我的附件文件名内容配置?
我正在使用针对4.5.2框架的ASP.NET Web API,并试图推出一个通过从表导出数据生成的CSV文件。在Firefox和Chrome中,一切正常,但在IE中(我用11测试),文件名被忽略,IE使用URL代替(没有扩展名)。我做错了什么?我怎样才能摆脱它 以下是我的控制器方法:Angularjs 为什么IE忽略我的附件文件名内容配置?,angularjs,internet-explorer,asp.net-web-api,web,http-headers,Angularjs,Internet Explorer,Asp.net Web Api,Web,Http Headers,我正在使用针对4.5.2框架的ASP.NET Web API,并试图推出一个通过从表导出数据生成的CSV文件。在Firefox和Chrome中,一切正常,但在IE中(我用11测试),文件名被忽略,IE使用URL代替(没有扩展名)。我做错了什么?我怎样才能摆脱它 以下是我的控制器方法: [HttpGet] public HttpResponseMessage ExportToCSV([FromUri]DistributionSearchCriteria criteria) { // Thi
[HttpGet]
public HttpResponseMessage ExportToCSV([FromUri]DistributionSearchCriteria criteria)
{
// This creates the csv in the temp folder and returns the temp file name
var file = _repository.ExportToCSV(criteria);
// This FileHttpResponseMessage is a custom type which just deletes the temp file in dispose
var result = new FileHttpResponseMessage(file, HttpStatusCode.OK)
{
Content = new StreamContent(File.OpenRead(file))
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "Distributions.csv"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
下面是自定义文件HttpResponseMessage
public class FileHttpResponseMessage : HttpResponseMessage
{
private string _filePath;
public FileHttpResponseMessage(string filePath, HttpStatusCode statusCode) : base(statusCode)
{
_filePath = filePath;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
Content.Dispose();
if (File.Exists(_filePath))
{
try
{
System.Diagnostics.Debug.WriteLine("Deleting {0}", (object)_filePath);
File.Delete(_filePath);
System.Diagnostics.Debug.WriteLine("{0} deleted", (object)_filePath);
}
catch
{
System.Diagnostics.Debug.WriteLine("Error Deleting {0}", (object)_filePath);
}
}
}
}
}
以下是我的AngularJS控制器中的两个JavaScript方法,用于启动下载:
vm.exportToCSV = function () {
var params = $httpParamSerializer(vm.searchCriteria);
$window.open(apiBase + 'Distribution/ExportToCSV?' + params, '_blank');
};
vm.exportAllToCSV = function () {
$window.open(apiBase + 'Distribution/ExportToCSV', '_blank');
};
从我在其他问题中读到的。。。设置附件;filename=应该足够IE使用。IE提示输入文件名“ExportToCSV”
我还尝试添加了一个伪参数,如?/distribution.csv
,它更改了下载文件名,但不是distribution.csv
,而是将
替换为.
,因此结果是distribution\u csv
。哦,我的痛苦
更新1:
我创建了一个单独的项目来解决这个问题,并提出了具体的解决办法。我尝试过在文件名前后加引号和不加引号,但仍然没有区别:
更新2:
因此,我认为我应该尝试“聪明”,尝试为具有扩展名的文件创建一个自定义HTTP处理程序:
Web.config
<!-- Route all files through asp -->
<modules runAllManagedModulesForAllRequests="true" />
<!-- Route all files through asp -->
<add name="FileDownloadHandler" path="/api/File/test.csv" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
正如预期的那样,它使用了
test.csv
,但它用
替换了
,从而产生了test\u csv
无扩展下载。这是我遇到的最奇怪的错误。我不确定是否应该归咎于我的病毒,但我的CPU最近达到了100%的最大值,这是因为McAFEE疯了,使我的系统陷入瘫痪。结果,我重新启动了我的机器。现在Internet Explorer正在按预期工作!我从来没有想到。。。我开始质疑我认为我知道的一切
故事的寓意:
config.Routes.MapHttpRoute(
name:"download",
routeTemplate: "api/File/test.csv",
defaults: new { controller = "File", action = "Get" }
);