如何在ASP网络核心中添加扩展过滤器?

时间:2020-04-01 12:29:07

标签: c# asp.net file-upload

我正在阅读有关该主题的几篇文章,但仍然无法使它起作用。

我想要的是一次或多次上传,您可以“避免”上传png,jpg和gif ...,而只允许txt或pdf ... 您在Upload files docs.microsoft给我的解决方案对我不起作用,所以我求助于您。

我附上我的控制器,我的模型和Index.cshtml:

模型

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace PortalDelPaciente.Models
{
    public class FileModel
    {
        [Required(ErrorMessage = "Please select file.")]
        [RegularExpression(@"([a-zA-Z0-9\s_\\.\-:])+(.png|.jpg|.gif)$", ErrorMessage = "Only Image files allowed.")]
        public IFormFile Photo { get; set; }
    }
}

控制器

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PortalDelPaciente.Models;

namespace PortalDelPaciente.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class ConfigController : Controller
    {

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult SingleFile(IFormFile file)
        {
            string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/assets/plugins/video");
            using (var fileStream = new FileStream(Path.Combine(SavePath, "archivito.jpg"), FileMode.Create, FileAccess.Write))
            {
                file.CopyTo(fileStream);

            }
            return RedirectToAction("Index");
        }

        public IActionResult MultipleFiles(IEnumerable<IFormFile> files)
        {
            int i = 0;
            foreach (var file in files)
            {
                string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/assets/plugins/video");
                 using (var fileStream = new FileStream(Path.Combine(SavePath, $"archivito{i++}.jpg"), FileMode.Create, FileAccess.Write))
                {
                    file.CopyTo(fileStream);
                }

            }
            return RedirectToAction("Index");
        }
    }
}

Index.cshtml

model PortalDelPaciente.Controllers.ConfigController

@{
    Layout = null;
}


@*
    To post files to server we need form with post method,
    and additionally browser must use multipart/form-data
    encoding. Otherwise file will not be sent.
*@
<h4>Single File upload</h4>
<form asp-controller="Config" asp-action="SingleFile"
      form method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <button type="submit">Upload</button>
</form>

<h4>Multiple File upload</h4>
<form asp-controller="Config" asp-action="MultipleFiles"
      form method="post" enctype="multipart/form-data">
    <input type="file" multiple name="files" />
    <button type="submit">Upload</button>
</form>

@*
    If upload succeeded we will show nice message to user.
*@
@if (ViewBag.Message != null)
{
    <div class="alert alert-success" style="margin-top:20px">
        @ViewBag.Message
    </div>
}

感谢所有帮助!

0 个答案:

没有答案
相关问题