使用ASP.NET Core Razor页面下载文件



澄清编辑我创建了一个新项目,试图将我原来的MVC项目转换为现在只使用Razor页面。我最初的解决方案是在这里。

我花了一段时间才完成转换以显示文档列表,但我现在已经完成了。我一直在尝试下载该文件,但它一直告诉我该文件不存在,尽管它已列出。

错误消息

No webpage was found for the web address: https://localhost:5001/FileShare/DownloadStub?id=SCHWADERER_PayStub_191018_1026.pdf

这是我的型号

FileDataModel.cs

public class FileDataModel
{
public string FileName { get; set; }
public string Size { get; set; }
public string DateModified { get; set; }
public string ParentDirName { get; set; }
public string SubDirName { get; set; }  
}

页面后面的我的代码

FileShare.cshtml.cs

public async Task<IActionResult> DownloadStub(string id)
{
using MemoryStream memoryStream = new MemoryStream();
string fileStorageConnection = _configuration.GetValue<string>("fileStorageConnection");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(fileStorageConnection);
CloudFileShare share = storageAccount.CreateCloudFileClient().GetShareReference("payreports");
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
CloudFileDirectory dir = rootDir.GetDirectoryReference(@"E000001/stubs");
CloudFile file = dir.GetFileReference(id);
await file.DownloadToStreamAsync(memoryStream);
Stream fileStream = file.OpenReadAsync().Result;
return File(fileStream, file.Properties.ContentType, file.Name);

}

最后我在网页上的代码

FileShare.cshtml

table class="table table-bordered">
<thead>
<tr>
<th>File Name</th>
<th>File Size</th>
<th>File Date</th>
<th>Download</th>
</tr>
</thead>
<tbody>
@foreach (var data in Model.FileDataModels)
{
<tr>
<td>@data.FileName</td>
<td>@data.Size</td>
<td>@data.DateModified</td>
<td><a class="btn btn-primary btn-sm"
href="/FileShare/DownloadStub?id=@data.FileName">Download</a></td>
</tr>
}
</tbody>
</table>

我是否没有将正确的值传递到href?

我还需要获取其他价值吗?

是否应该使用taghelper来完成此操作?

我不知道发生了什么,我需要做什么,这样我才能朝着正确的方向前进。如有任何提示,我们将不胜感激!

错误消息说它找不到你的url,与文件无关。默认情况下,url为Controller/Action/Parameter,而不是查询字符串,因此您的url应为/FileShare/DownloadStub/@data.FileName

但我建议使用RAZOR,因为如果你将来自定义URL的创建方式,即使不确定使用什么作为控制器名称(可能是FileShare(,它也可以让你正常工作:

@Html.ActionLink("Description", "DownloadStub", "FileShare", new { id = data.FileName}, null);

最新更新