我们已经实现了一些下载功能。我们正在 api 端设置内容配置。通过这样做,我们的 UI 将从内容处置中获取文件名。
在添加 SSL 证书并将站点移至特定于域的链接之前,此方法一直工作良好。
现在,内容配置返回null
,并且以null
作为名称且没有扩展名下载文件。
以下是我们下载文件的代码。
public IActionResult DownloadFile(int ID)
{
try
{
if (ValidateUsrToken())
{
using (ExcelPackage package = BAL.DownloadFile(ID))
{
string fileName = Uri.EscapeDataString(package.File.Name);
System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
{
FileName = fileName,
};
Response.Headers.Add("Content-Disposition", cd.ToString());
return File(package.GetAsByteArray(), "application /vndopenxmlformats-officedocument.spreadsheetml.sheet");
}
}
else throw new NullReferenceException();
}
catch (Exception ex)
{
throw ex;
}
}
UI 代码
const disposition = res.headers.get('content-disposition') || '';
if (disposition && disposition.indexOf('attachment') !== -1) {
const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
const matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) {
filename = matches[1].replace(/['"]/g, '');
filename = decodeURIComponent(filename);
}
}
我们的 api 和 UI 在 Azure 云上实现。
任何帮助对此表示赞赏。