首页 > 解决方案 > 为什么我的页面刷新而不是在剃须刀页面中下载文件?

问题描述

我在我的一个剃须刀页面中有一个表单,提交时会生成 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"
    };
}

我不明白我在这里缺少什么。

编辑:处理程序被成功调用。如果我在其中设置断点并进行调试,则处理程序成功完成,但没有文件发送到浏览器。

标签: asp.net-corerazor-pages

解决方案


好的,问题在于使用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)

推荐阅读