gpt4 book ai didi

javascript - Safari 添加 .html 以使用 epplus jquery.fileDownload.js 下载 xlsx

转载 作者:行者123 更新时间:2023-11-28 18:17:58 24 4
gpt4 key购买 nike

我正在使用 MVC c# 代码生成 excel 文件,然后下载,我正在使用 EPPLUS,一切正常,除了 Mac 上的 Safari 上,当我尝试创建文件时,下载时在末尾添加了 .html ,例如,file.xlsx.html我不知道如何解决这个问题。

    $.fileDownload(url, {
httpMethod: 'POST',
data: dtColumns,
successCallback: function() {
//code
},
failCallback: function() {
//code

}
});

公共(public)流导出数据集(IEnumerable数据集,IList columnsToExport,字符串模板) { //分组适用于匿名类型,因此我们可以将导出分组到它们自己的表中 var groupings = dataset.GroupBy(i => i.GetType());

        using (var package = new ExcelPackage(new FileInfo(GetTemplateFilePath(template)), true))
{
var ws = package.Workbook.Worksheets[1];

if (groupings.Any())
{
// add each "anon type matched grouping"
foreach (var grouping in groupings)
{
// because of EPP inheritance bug of T, we can just use dataTable
var dataTable = new DataTable(grouping.Key.Name);
var properties = grouping.Key.GetProperties(); // Get anon type Properties
var columns = columnsToExport.OrderBy(x => x.Position);

foreach (var property in columns.Where(column => column.IsVisible).SelectMany(column => properties.Where(property => property.Name.Equals(column.Name))))
{
dataTable.Columns.Add(property.Name);
}

foreach (var item in grouping.ToList())
{
var dataRow = dataTable.NewRow();

foreach (var p in columns.Where(column => column.IsVisible).SelectMany(column => properties.Where(property => property.Name.Equals(column.Name))))
{
dataRow[p.Name] = p.GetValue(item);
}

dataTable.Rows.Add(dataRow);
}

ws.Cells[1, 1].LoadFromDataTable(dataTable, PrintHeaders: true);
ws.Cells.AutoFitColumns();
}
}
else
{
// This case is when there is no data on the table to load the Excel so we create an empty sheet but we add the headers
var datasetStructure = dataset.GetType().GetGenericArguments()[0];

// because of EPP inheritance bug of T, we can just use dataTable
var dataTable = new DataTable(datasetStructure.Name);
var properties = datasetStructure.GetProperties(); // Get anon type Properties

foreach (var property in properties)
{
dataTable.Columns.Add(property.Name);
}

ws.Cells[1, 1].LoadFromDataTable(dataTable, PrintHeaders: true);
ws.Cells.AutoFitColumns();
}

var fileStream = new MemoryStream();
package.SaveAs(fileStream);

return fileStream;
}
}

在 Controller 上:

返回新的CustomFileResult(fileStream, fileName);

公共(public)类 CustomFileResult :IHttpActionResult { 私有(private)只读流_fileContent; 私有(private)只读字符串_fileName;

    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);
}
}

最佳答案

解决方案是在响应中添加 excel 的 header ContentType (application/vnd.ms-excel)。默认为 text/HTML,因此 Safari 无法识别包含 excel 文件的响应,因此添加以下行:

response.Content.Headers.ContentType  = new MediaTypeHeaderValue("application/vnd.ms-excel");

问题已解决。

关于javascript - Safari 添加 .html 以使用 epplus jquery.fileDownload.js 下载 xlsx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40517017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com