在asp.net上完全没有知识,请尝试在这里自学ASP.NET Core 2.0 MVC。
我目前所坚持的是:
尝试找到用于图像上传的教程,以将图像放置到根目录的子目录中,例如/ images / items,然后将该路径保存到数据库中,以便在列出项目时也可以将图像img src。
到目前为止,这是我所做的:
ItemsController:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,SupplierId,CategoryId,Name,Description,Image,Price,Enabled,DateAdded")] Item item)
{
if (ModelState.IsValid)
{
// add image upload
// end image upload
_context.Add(item);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(item);
}
Run Code Online (Sandbox Code Playgroud)
我的ItemModel类是:
public class Item
{
public int Id { get; set; }
public int SupplierId { get; set; }
public int CategoryId { get; set; }
public string Name { get; set; }
public string Description { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 ASP.NET Core Web Api 上传文件。正如我发现的那样,这段代码:
namespace ModelBindingWebSite.Controllers
{
public class HomeController : Controller
{
private IHostingEnvironment _environment;
public HomeController(IHostingEnvironment environment)
{
_environment = environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Index(ICollection<IFormFile> files)
{
var uploads = Path.Combine(_environment.WebRootPath, "uploads");
foreach (var file in files)
{
if (file.Length > 0)
{
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
await file.SaveAsAsync(Path.Combine(uploads, fileName));
}
}
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误IFormFile不包含定义SaveAsASync并且没有扩展方法。任何想法?