asp.net-core - 为什么我的页面刷新而不是在剃须刀页面中下载文件?
问题描述
我在我的一个剃须刀页面中有一个表单,提交时会生成 abyte[]
并且我返回 a FileStreamResult
。问题是,所发生的只是页面刷新,并且没有提示我下载文件。
这是我的表格:
<form autocomplete="off" method="post" asp-page-handler="attendance" asp-route-id="@Model.Data.Id">
<div class="form-group">
<label class="font-weight-bold">Date of meeting</label>
<input class="form-control datepicker" id="meeting-date" type="text" value="DateOfMeeting" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">Download document</button>
</div>
</form>
这是我的页面处理程序:
public async Task<IActionResult> OnPostAttendanceAsync(int id)
{
var query = new AttendanceList.Query {
Date = DateOfMeeting,
SchoolId = id
};
var model = await _mediator.Send(query);
var stream = new MemoryStream(model.Data);
return new FileStreamResult(stream, ContentType.Pdf) {
FileDownloadName = "Attendance.pdf"
};
}
我不明白我在这里缺少什么。
编辑:处理程序被成功调用。如果我在其中设置断点并进行调试,则处理程序成功完成,但没有文件发送到浏览器。
解决方案
好的,问题在于使用POST
而不是GET
作为表单方法。
我已将代码更新为以下内容,现在出现下载提示,一切正常:
<form autocomplete="off" method="get" asp-route-id="@Model.Data.Id">
<input type="hidden" name="handler" value="attendance" />
<div class="form-group">
<label class="font-weight-bold">Date of meeting</label>
<input class="form-control datepicker" type="text" name="dateOfMeeting" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">Download document</button>
</div>
</form>
处理程序签名现在是:
public async Task<IActionResult> OnGetAttendanceAsync(int id, string dateOfMeeting)
推荐阅读
- ruby-on-rails - 推送到 Heroku 时 webpacker 不会编译
- asp.net-mvc - 实体框架创建一个空的迁移文件
- c# - 如何在死亡和级别重置之间添加等待时间。C#
- javascript - 加载 ecmascript 模块(.mjs 文件)作为 nodejs cli 的主要入口
- html - 我无法移动图像,我该怎么办?
- purescript - purescript 卤素:将 IProp 附加到 HTML
- c# - 有没有办法将类的实际实例作为参数传递?
- java - 如何正确使用 loadtest4j
- web-scraping - 如何使用scrapy刮掉隐藏在下拉菜单中的链接?
- php - Laravel if 语句用于各种编号项目以更改样式