【发布时间】:2019-06-25 21:43:25
【问题描述】:
我正在开发文档管理系统,我已成功完成上传和删除文件,但我无法执行从数据库下载操作,我是 Razor Pages 的新手,所以如果有人帮助我,将不胜感激。下面是关于我的项目的更多详细信息。
这是我的 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(代码隐藏)
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");
}
}
}
【问题讨论】:
-
你需要添加文件的路径,你可以指定下载链接在哪里,例如
<a href="https://www.example.com/CV.pdf" target="_blank" >Download CV</a> -
我完全不知道如何编写下载操作的代码,所以请你给我这个操作的全貌。这是我的下载
link button<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>我想从数据库执行下载 -
我已经在答案中发布了链接。请看一看。
标签: c# asp.net-core-2.0 razor-pages