[英]Trouble adding file upload feature to project - ASP.Net MVC Core 3.0
Please forgive me if I forgot some key piece of information, I am a pretty new developer and this is my first posting.如果我忘记了一些关键信息,请原谅我,我是一个相当新的开发人员,这是我的第一次发帖。
I am attempting to add a feature to my ASP.Net MVC 5 Core 3 project where the user can upload a file to ~/wwwroot/PromotionImages but I am having trouble finding many resources for Core 3.0我正在尝试向我的 ASP.Net MVC 5 Core 3 项目添加一个功能,用户可以在其中将文件上传到 ~/wwwroot/PromotionImages 但我无法找到许多 Core 3.0 资源
Best solution is probably to start over using an older version of Core where I will find more resources, but I am pretty far into this project already.最好的解决方案可能是使用旧版本的 Core 重新开始,在那里我会找到更多资源,但我已经很深入这个项目了。
I have read that MapPath no longer works in Core 3, but I am not really sure what replaces it.我已经读到 MapPath 在 Core 3 中不再有效,但我不确定是什么取代了它。 WebRootPath is a property of IWebHostEnvironment but I'm not sure how to implement that. WebRootPath 是 IWebHostEnvironment 的一个属性,但我不确定如何实现它。
I receive the following three errors that I haven't been able to work through.我收到以下三个我无法解决的错误。
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促销控制器.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);
}
For map path, you can use the System.IO.Path class.对于地图路径,您可以使用 System.IO.Path 类。 Here is some code which can be the starting point.这是一些可以作为起点的代码。
View code查看代码
<form method="post" enctype="multipart/form-data">
<input type="File" name="file" />
<input type="submit" value="Upload" />
</form>
And here is the controller code.这是控制器代码。
[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();
}
First I am creating the path of the images
folder.首先,我正在创建images
文件夹的路径。 Next, I am looking for the file extension.接下来,我要查找文件扩展名。 Then I am generating a unique file name with the extension and saving the file.然后我生成一个带有扩展名的唯一文件名并保存文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.