我正在使用文档管理系统,我已成功完成上传和删除,但是我无法执行从数据库下载操作,我是Razor Pages的新手,所以如果有人帮助我,那么我将非常感激。bellow是有关我的项目的更多详细信息。
这是我的Index.cshtml
<div>
<table class="table " style="background-color:lightskyblue;" >
<thead style="font-weight:bold ;color:white;background-color:black;margin-right:-50px;padding-right:80px;">
<tr style="background-color:darkblue;color:white;">
<th>
@Html.DisplayNameFor(model => model.Schedule[0].Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Schedule[0].UploadDT)
</th>
<th>
@Html.DisplayNameFor(model => model.Schedule[0].PublicScheduleSize)
</th>
@*<th class="text-center">
@Html.DisplayNameFor(model => model.Schedule[0].PrivateScheduleSize)
</th>*@
<th class="text-center">Operations</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Schedule)
{
<tr style="font-weight:bold">
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.UploadDT)
</td>
<td>
@Html.DisplayFor(modelItem => item.PublicScheduleSize)
</td>
<td style="margin-left:-60px;">
@*<a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>*@
<a asp-page="./Delete" asp-route-id="@item.ID" class="btn btn-danger glyphicon glyphicon-trash" role="button">Delete</a>
</td >
<td >
<a asp-page="./Download" download="item.UploadDT" asp-route-id="@item.ID" class="btn btn-primary btn-sm glyphicon glyphicon-download-alt " role="button">Download</a>
</td>
</tr>
}
</tbody>
</table>
</div>
这是PageModel(Codebehind)
namespace DMS.Pages.Schedules
{
public class IndexModel : PageModel
{
private readonly DMS.Models.DatabaseContext _context;
public IndexModel(DMS.Models.DatabaseContext context)
{
_context = context;
}
[BindProperty]
public FileUpload FileUpload { get; set; }
public IList<Schedule> Schedule { get; private set; }
public async Task OnGetAsync()
{
Schedule = await _context.Schedule.AsNoTracking().ToListAsync();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
Schedule = await _context.Schedule.AsNoTracking().ToListAsync();
return Page();
}
var publicScheduleData = await FileHelpers.ProcessFormFile(FileUpload.UploadPublicSchedule, ModelState);
FileHelpers.ProcessFormFile(FileUpload.UploadPrivateSchedule, ModelState);
ProcessFormFile method
if (!ModelState.IsValid)
{
Schedule = await _context.Schedule.AsNoTracking().ToListAsync();
return Page();
}
var schedule = new Schedule()
{
PublicSchedule = publicScheduleData,
PublicScheduleSize = FileUpload.UploadPublicSchedule.Length,
FileUpload.UploadPrivateSchedule.Length,
Title = FileUpload.Title,
UploadDT = DateTime.UtcNow
};
_context.Schedule.Add(schedule);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
}
答案 0 :(得分:0)
您的代码很难解析,但是看来最终您将文件作为Blob存储在数据库表中。一般来说,您需要执行一个操作以从数据库中检索此数据并将其作为FileResult
返回:
public OnDownloadGetAsync(int scheduleId)
{
var schedule = await _context.Set<Schedule>().FindAsync(scheduleId);
if (schedule == null)
return NotFound();
return File(schedule.PublicSchedule, schedule.PublicScheduleType);
}
File
的第二个参数是mime类型(即text/csv
,application/vnd.excel
,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
等)。如果您知道它始终是同一类型的文件,则可以对其进行硬编码,但是理想情况下,还应该将其持久保存在数据库中(这就是我“创建” PublicScheduleType
属性的原因坚持下去)。显然,您显然需要修改上传代码,以保留要上传文件的内容类型。
类似于excel电子表格之类的东西都应强制进行下载,但是默认情况是将文件作为“内联”传递,或尝试在浏览器中显示。如果您希望始终强制下载,则可以向File
提供第三个参数,并提供文件名:
return File(schedule.PublicSchedule, schedule.PublicScheduleType, "schedule.xlsx");
设置完成后,您只需链接到此处理程序即可:
<a asp-page="Index" asp-handler="Download" asp-route-scheduleId="@schedule.Id">Download</a>