Javascript Safari添加.html以使用epplus jquery.fileDownload.js下载xlsx
我用MVC代码生成一个excel文件,然后下载,我用的是EPPLUS,除了Mac上的Safari,其他一切都很好,当我尝试在下载文件时创建文件,最后添加了.html,例如file.xlsx.html 我不知道该怎么解决这个问题Javascript Safari添加.html以使用epplus jquery.fileDownload.js下载xlsx,javascript,c#,jquery,excel,model-view-controller,Javascript,C#,Jquery,Excel,Model View Controller,我用MVC代码生成一个excel文件,然后下载,我用的是EPPLUS,除了Mac上的Safari,其他一切都很好,当我尝试在下载文件时创建文件,最后添加了.html,例如file.xlsx.html 我不知道该怎么解决这个问题 $.fileDownload(url, { httpMethod: 'POST', data: dtColumns, successCallback: function() { //code
$.fileDownload(url, {
httpMethod: 'POST',
data: dtColumns,
successCallback: function() {
//code
},
failCallback: function() {
//code
}
});
公共流导出数据集(IEnumerable数据集、IList columnsToExport、字符串模板)
{
//分组对anon类型有效,因此我们可以将导出分组到它们自己的表中
var groupings=dataset.GroupBy(i=>i.GetType())
在控制器上:
返回新的CustomFileResult(文件流,文件名)
公共类CustomFileResult:IHttpActionResult
{
私有只读流_fileContent;
私有只读字符串\u文件名
public CustomFileResult(Stream fileContent, string fileName)
{
_fileContent = fileContent;
_fileName = fileName;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.Run(() =>
{
var response = new HttpResponseMessage();
var fileDownloadFail = false;
if (_fileContent == null || _fileContent.Length == 0)
{
response.StatusCode = HttpStatusCode.InternalServerError;
fileDownloadFail = true;
}
else
{
response.StatusCode = HttpStatusCode.OK;
_fileContent.Position = 0;
response.Content = new StreamContent(_fileContent);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = _fileName,
};
}
//Required for plugin jquery.fileDownload.js
var cookie = new CookieHeaderValue("fileDownload", "true") { Path = "/" };
response.Headers.AddCookies(new CookieHeaderValue[] { cookie });
return response;
}, cancellationToken);
}
}
公共CustomFileResult(流文件内容、字符串文件名)
{
_fileContent=fileContent;
_fileName=文件名;
}
公共任务执行同步(CancellationToken CancellationToken)
{
返回任务。运行(()=>
{
var response=新的HttpResponseMessage();
var fileDownloadFail=false;
如果(_fileContent==null | | _fileContent.Length==0)
{
response.StatusCode=HttpStatusCode.InternalServerError;
fileDownloadFail=true;
}
其他的
{
response.StatusCode=HttpStatusCode.OK;
_fileContent.Position=0;
response.Content=新的StreamContent(\u fileContent);
response.Content.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“附件”)
{
文件名=\u文件名,
};
}
//插件jquery.fileDownload.js需要
var cookie=new-CookieHeaderValue(“文件下载”,“真”){Path=“/”};
AddCookies(新的CookieHeaderValue[]{cookie});
返回响应;
},取消令牌);
}
}
解决方案是在响应上添加excel(
application/vnd.ms excel
)的标题ContentType
。默认值为text/HTML
,因此Safari不理解响应包含excel文件,因此添加此行:
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
问题已解决。解决方案是在响应上添加excel(
application/vnd.ms excel
)的标题ContentType
。默认值为text/HTML
,因此Safari不理解响应包含excel文件,因此添加此行:
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
问题已解决。解决方案是在响应上添加内容类型:解决方案是在响应上添加内容类型: