我正在尝试从我的文件共享门户下载文件。我使用的是数据表,下载在第一页上工作正常,但如果我尝试从应用程序的任何其他页面下载文件。下载文件并在浏览器中显示空白页面。
这是下载方法:
[Route("DownloadFile")]
public async Task<FileResult> DownloadFile(Guid id)
{
var path = await getDocumentFullPath(id);
var fileName = await getDocumentTitle(id);
if (string.IsNullOrWhiteSpace(fileName))
throw new Exception("Please select a file to download.");
if (!System.IO.File.Exists(path))
throw new FileNotFoundException();
var provider = new FileExtensionContentTypeProvider();
if (!provider.TryGetContentType(path, out var contenttype))
{
contenttype = "application/octet-stream";
}
var bytes = await System.IO.File.ReadAllBytesAsync(path);
return File(bytes, contenttype, fileName);
}
.cshtml
文件:
<table id="documents" class="table table-striped" style="width:100%">
<thead>
<tr>
<th>Title</th>
<th>Date</th>
<th>Document Code</th>
<th>Extension</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
<a class="action-icon docTypes" onclick="location.href='@Url.Action("DownloadFile", "Document", new { id = item.DocumentId })'">@item.Title</a>
</td>
<td>@item.DocumentDate.ToString("dd-MMM-yyyy")</td>
<td>@item.DocumentCode</td>
<td>@item.Extension</td>
<td>
<a asp-action="Update" asp-route-Id="@item.DocumentId" class="action-icon"> <i class="fa fa-edit m-1"></i></a>
<a asp-action="Details" asp-route-Id="@item.DocumentId" class="action-icon"> <i class="fa fa-thin fa-list m-1"></i></a>
<a asp-action="Delete" asp-route-Id="@item.DocumentId" class="action-icon"> <i class="fa-regular fa-trash-can m-1"></i></a>
</td>
</tr>
}
</tbody>
</table>
@section scripts {
<script>
new DataTable('#documents');
$(".docTypes").hover(function () {
$(this).css('cursor', 'pointer');
}, function () {
$(this).css('cursor', 'auto');
});
</script>
}
该项目正在使用 ASP.NET Core 7。
我先尝试过这个方法:
<a asp-action="DownloadFile"
asp-controller="Document"
asp-route-id="@item.DocumentId">
@item.Title
</a>
而不是:
<a class="action-icon docTypes"
onclick="location.href='@Url.Action("DownloadFile", "Document", new { id = item.DocumentId })'">@item.Title</a>
但不幸的是,它不起作用,我也在谷歌上搜索了这个问题,但没有任何运气......
我正在尝试从我的文件共享门户下载文件。我正在使用一个 数据表,下载在第一页上工作正常,但如果我尝试 从应用程序的任何其他页面下载文件。下载文件并 在浏览器中显示空白页面
在调用 DownloadFile 控制器操作时,您是否检查了路径和文件名中的内容?。另一点是, File(bytes, contenttype, fileName) 是否返回了您期望的数据?根据您共享的代码,我认为您的前端代码没有问题,如果填充了正确的响应,请详细调试您的 DownloadFile 操作。
关于您下载的 DOM,您可以执行以下操作:
<a asp-controller="DownloadAll" asp-action="DownloadFile" class="btn btn-info" asp-route-imageName="du.png">Download</a>
注意:我使用了字符串作为参数,因为您使用了GUID,请确保它是正确的并且您的
asp-route
在控制器操作中包含匹配的值。
工作样本:
控制器动作:
[Route("DownloadFile")]
public async Task<FileResult> DownloadFile(string imageName)
{
var path = Path.GetFullPath("./wwwroot/ImageName/Cover/" + imageName);
MemoryStream memory = new MemoryStream();
using (FileStream stream = new FileStream(path, FileMode.Open))
{
await stream.CopyToAsync(memory);
}
memory.Position = 0;
return File(memory, "image/png", Path.GetFileName(path));
}
注意:我已经使用图像文件进行了测试,请确保您的相关GUID包含正确的数据。
输出:
最后,根据您的场景,我建议您检查一下路由值和对应的Id是否有效并且有相关的值。