无法将文件上传功能添加到项目 - ASP.Net MVC Core 3.0
Trouble adding file upload feature to project - ASP.Net MVC 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);
}
地图路径,可以使用System.IO.Pathclass。这是一些可以作为起点的代码。
查看代码
<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
文件夹的路径。接下来,我正在寻找文件扩展名。然后我生成一个带有扩展名的唯一文件名并保存文件。
如果我忘记了一些关键信息,请原谅我,我是一个非常新的开发人员,这是我的第一篇文章。
我正在尝试向我的 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);
}
地图路径,可以使用System.IO.Pathclass。这是一些可以作为起点的代码。
查看代码
<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
文件夹的路径。接下来,我正在寻找文件扩展名。然后我生成一个带有扩展名的唯一文件名并保存文件。