Как проверить загрузку файла, чтобы принять только .PDF - PullRequest
0 голосов
/ 25 марта 2020

У меня проблемы с проверкой формы загрузки, чтобы принимать только PDF-документы. Кто-нибудь может подсказать, как я могу go реализовать это в своем заявлении IF? Я также планирую разрешить только размер файла до 50 МБ, любой совет для этого будет принята с благодарностью. Ниже я включил свой индекс. html .cs и индекс. html. Заранее спасибо!

namespace StoriesApplication.Pages.Stories
{
    public class IndexModel : PageModel
    {
        private readonly StoriesApplication.Models.StoriesApplicationContext _context;

        public IndexModel(StoriesApplication.Models.StoriesApplicationContext context, IHostingEnvironment environment, IFileProvider fileProvider)
        {
            _context = context;
            _environment = environment;
            _fileProvider = fileProvider;

        }

        public IList<StoryInfo> StoryInfo { get; set; }

        public async Task OnGetAsync()
        {
            StoryInfo = await _context.StoryInfo.ToListAsync();
        }

        private IHostingEnvironment _environment;
        private readonly IFileProvider _fileProvider;

        public ActionResult OnPostDownloadFile()
        {

            var UploadName = Request.Form["Title"];

            if (string.IsNullOrEmpty(UploadName))
            {
                ViewData["Empty"] = "No File has been submitted";
                return RedirectToPage();
            }

            else

            {
                string contentRootPath = _environment.ContentRootPath + "/wwwroot/Uploads";
                return PhysicalFile(Path.Combine(contentRootPath, UploadName), "text/plain", UploadName);
            }



        }
    }
}

@page
@model StoriesApplication.Pages.Stories.IndexModel

@{
    ViewData["Title"] = "Index";
}

Вот соответствующий Inedx. html

<h1>Stories</h1>

<p>
    <a asp-page="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.StoryInfo[0].Title)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.StoryInfo[0].Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.StoryInfo[0].Genre)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.StoryInfo[0].FileName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.StoryInfo[0].Description)
            </th>
            <th></th>
            <
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.StoryInfo)
        {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Genre)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FileName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                <a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
                <a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
                <a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
            </td>
        </tr>

        }
    </tbody>
</table>

<form method="post">
    <input name="Title" type="Text" class="StoryInputBox" placeholder="FileName" id="Title">
    <button type="submit" class="BtnStories" asp-page-handler="DownloadFile">Download</button>
    @*<input class="BtnStories" type="submit" name="submit" value="Submit">//*@
</form>

@ViewData["Empty"]

...