我无法履行在asp.net 2.0核心剃刀网页下载操作

问题描述 投票:0回答:1

我工作的文件管理系统,但无法通过SQL数据库在asp.net核心执行下载操作2.0剃刀页,波纹管是我的项目的更多细节。

Index.cshtml

@page
@model IndexModel
@{
    ViewData["Title"] = "Schedules";
}

<div style="background-color:aliceblue;">
    <hr />

    <div class="well well-sm " style="background-color:lightskyblue;height:50px;"><h3 style="text-align:center;color:white;font-weight:bold;margin-top:0px;"> +Add Files</h3></div>

    <div class="row">
        <div class="col-md-4">
            <form method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label asp-for="FileUpload.Title" class="control-label"></label>
                    <input asp-for="FileUpload.Title" type="text" class="form-control" />
                    <span asp-validation-for="FileUpload.Title" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="FileUpload.UploadPublicSchedule" class="control-label"></label>
                    <input asp-for="FileUpload.UploadPublicSchedule" type="file" class="form-control                        btn btn-primary btn-sm float-left" style="height:auto" />
                    <span asp-validation-for="FileUpload.UploadPublicSchedule" class="text-                     danger"></span>
                </div>
               <input type="submit" value="Upload" class="btn btn-primary" />
            </form>
        </div>
    </div>
    <hr />
    <h3>Loaded Schedules</h3>
    <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-handler="karuna.txt" role="button"
                           download>
                            download the license
                        </a>
                    </td>
                   </tr>
            }
        </tbody>
    </table>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
<script>
    function createPDFLink(fileName) {
        var doc = new pdf();
        // whatever content you want to download
        var a = document.createElement("a");
        a.download = fileName;
        a.title = "download as PDF";
        a.href = doc.output('datauri', { "fileName": name });
        return a;
    }

    // some paragraph in the page
    document.querySelector(
        "p.saveaspdf"
    ).appendChild(createPDFLink(
        "document-" + document.title + ".pdf"
    ));
</script>

这是页型号:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using DMS.Models;
using DMS.Utilites;
using System.IO;
using System.Runtime.InteropServices.ComTypes;

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);

            if (!ModelState.IsValid)
            {
                Schedule = await _context.Schedule.AsNoTracking().ToListAsync();
                return Page();
            }

            var schedule = new Schedule()
            {
                PublicSchedule = publicScheduleData,
                PublicScheduleSize = FileUpload.UploadPublicSchedule.Length,
                Title = FileUpload.Title,
                UploadDT = DateTime.UtcNow
            };

            _context.Schedule.Add(schedule);
            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");
        }
    }
}
  • 我试图从数据库下载的所有文件。
  • 所有页面都连接到数据库。
c# asp.net-core-2.0 razor-pages
1个回答
0
投票

我不知道正是你正在尝试做的。你页面有文件上传和下载东西的混合。

对于文件下载我这样做。在.cshtml网页我有一个按钮将调用页面背后的代码后处理程序。喜欢:

<button id='@("download"+item.ID)' asp-page-handler="DownloadFile" 
  asp-route-externalId="@item.ExternalId" 
  class="btn btn-primary">Download Report
</button>

然后在后面的代码我有一个处理程序,在我的负荷从数据库的报告得到的所有文件的zip文件,然后返回到触发下载的浏览器。最重要的部分是从处理程序与路径磁盘上的文件返回PhysicalFile

    /// <summary>
    /// Return the zip file for the requested id.
    /// </summary>
    /// <param name="externalId">Report external id</param>
    public async Task<IActionResult> OnPostDownloadFileAsync(string externalId) {

        var report = LoadReport();
        if (report == null) {
            return Page();
        }

        var zipFile = report.GetReportDownload();
        string filename = FileUtils.WebSafeFileName(zipFile, ".zip");
        return PhysicalFile(zipFile, "application/zip", filename);
    }
© www.soinside.com 2019 - 2024. All rights reserved.