为什么我的页面刷新而不是在 razor 页面中下载文件?
Why does my page refresh instead of downloading the file in razor page?
我的一个 razor-pages 中有一个表单,提交后会生成一个 byte[]
和 return 一个 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)
我的一个 razor-pages 中有一个表单,提交后会生成一个 byte[]
和 return 一个 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)