[英]I'm unable to perform download operation in asp.net core 2.0 razor pages
[英]How to perform Download Operation using asp.net core 2.0 Razor Page
我正在使用文檔管理系統 ,我已經成功完成了文件的上傳和刪除 ,但是我無法執行從數據庫進行的下載操作,我是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");
}
}
}
您的代碼很難解析,但是似乎最終您將文件作為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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.