无法将文件上传功能添加到项目-ASP.Net MVC Core 3.0

时间:2019-12-15 02:15:50

标签: c# asp.net-core asp.net-mvc-5 asp.net-core-3.0

如果我忘记了一些关键信息,请原谅我,我是一个相当新的开发人员,这是我的第一篇帖子。

我正在尝试向我的ASP.Net MVC 5 Core 3项目添加功能,用户可以在其中将文件上传到〜/ wwwroot / PromotionImages,但是我在查找Core 3.0的许多资源时遇到了麻烦

最好的解决方案可能是从使用较旧版本的Core开始,在该版本中我会找到更多的资源,但是我对这个项目已经很远了。

我已经阅读到MapPath在Core 3中不再可用,但是我不确定是什么替代了它。 WebRootPath是IWebHostEnvironment的属性,但我不确定如何实现。

我收到以下三个无法解决的错误。

CS1061  'IWebHostEnvironment' does not contain a definition for 'MapPath' and no accessible extension method 'MapPath' accepting a first argument of type 'IWebHostEnvironment' could be found (are you missing a using directive or an assembly reference?)

CS0119  'ControllerBase.File(byte[], string)' is a method, which is not valid in the given context

CS0103  The name 'FileMode' does not exist in the current context

Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration, IWebHostEnvironment environment)
        {
            Configuration = configuration;
            this.Environment = environment;
        }

        public IConfiguration Configuration { get; }
        public IWebHostEnvironment Environment { get; }

PromotionsController.cs


public class PromotionsController : Controller
    {

        private readonly IWebHostEnvironment hostingEnvironment;
        private readonly MonitorContext _context;

        public PromotionsController(MonitorContext context, IWebHostEnvironment hostingEnvironment)
        {
            _context = context;
            this.hostingEnvironment = hostingEnvironment;
        }
[HttpPost]
[ValidateAntiForgeryToken]

        public async Task<IActionResult> Create([Bind("PromoId,MonitorId,PromoTitle,PromoPath")] Promotion promotion)
        {
            if (ModelState.IsValid)
            {
                var relativeWebPath = $"/PromotionImages/{promotion.PromoFile.FileName}";
                var filePath = this.hostingEnvironment.MapPath($"~/wwwroot/{relativeWebPath}");
                using (var fileStream = File.Open(filePath, FileMode.OpenOrCreate))
                {
                    promotion.PromoFile.CopyTo(fileStream);
                }
                promotion.PromoPath = relativeWebPath;

                _context.Add(promotion);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(promotion);
        }

1 个答案:

答案 0 :(得分:0)

对于映射路径,可以使用System.IO.Path类。这是一些可以作为起点的代码。

查看代码

<form method="post" enctype="multipart/form-data">
    <input type="File" name="file" />
    <input type="submit" value="Upload" />
</form>

这是控制器代码。

[HttpPost]
public IActionResult Index(IFormFile file)
{
    var imagePath = Path.Combine(_hostEnvironment.WebRootPath, "images");

    var uploadedFileExtn = Path.GetExtension(file.FileName);
    var fileName = Path.ChangeExtension(Guid.NewGuid().ToString("N"), uploadedFileExtn);
    using (var stream = System.IO.File.OpenWrite(Path.Combine(imagePath, fileName)))
    {
        file.CopyTo(stream);
    }

    return View();
}

首先,我正在创建images文件夹的路径。接下来,我正在寻找文件扩展名。然后,我以扩展名生成唯一的文件名并保存文件。