ASP.NET核心文件下载没有进展



我有一个ASP。NET Core 6应用程序,具有zip dowload功能。这是我的后端代码:

[HttpGet("/api/DownloadCenterController/DownloadDataset/{fileName}")]
public IActionResult DownloadDataset(string fileName)
{
dynamic response = new ExpandoObject();
try
{
response.status = "200";
var path = ConfigManager.GetDownloadCenterFinishedZippedDataSetsDirectory() + $"{fileName}";
var fileInfo = new FileInfo(path);
this.Response.ContentLength = fileInfo.Length;
var zipBytes = System.IO.File.ReadAllBytes(path);
return File(zipBytes, "application/octet-stream", "export.zip");
}
catch (Exception ex)
{
response.status = "400";
response.message = "Couldn't download the zip file, error in logs";
_logger.LogError(ex, "Error download the data set:");
}
return Json(response);
}

这是jquery:

// Starts the download of a dataset zip file
async function downloadDataZip(filename) {
try {
$.ajax({
type: "GET",
url: "/api/DownloadCenterController/DownloadDataset/" + filename,
//contentType: "application/json",
contentType: "application/octet-stream",
xhrFields: {
responseType: 'blob'
},
//data: JSON.stringify(obj),
success: async function (data) {
var a = document.createElement('a');
var url = window.URL.createObjectURL(data);
a.href = url;
a.download = 'filename.zip';
document.body.append(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
});
} catch (error) {
console.error(error);
return undefined;
}
}

下载工作正常,下载的200MB zip文件没有损坏,但浏览器没有任何进展。所以目前,我按下下载按钮-什么都没有";发生";大约15秒,然后浏览器打开下载选项卡,显示下载进度一毫秒,然后砰的一声,文件已经下载完毕。所以我假设文件正在下载,但浏览器没有显示任何进度,而是只有在下载完成时才会弹出。

我做错了什么?我想要";正常的";在浏览器中下载进度。

提前感谢!

我在@pcalkins 的帮助下修复了它

我去掉了整个jquery代码,只使用了按钮上的asp标签,如下所示:

<a class="btn btn-warning card-shadow-light start-download-btn"
asp-controller="DownloadCenter" asp-action="DownloadDataset" asp-route-fileName="@Model.ZipFileViewModel.FileName">
<i class="fas fa-download xxlarge-font text-light"></i>
</a>

主计长保持不变。

谢谢大家!

最新更新