using Microsoft.AspNetCore.Mvc.RazorPages; using Snootalogue.Data; using Microsoft.AspNetCore.Mvc; using System.Net; using System.Linq; using System.IO; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Snootalogue.ViewModels; using Snootalogue.Models; namespace Snootalogue.Pages { public class UploadModel : PageModel { private readonly SnootalogueContext _context; [BindProperty] public UploadDocument ud { get; set; } public UploadModel(SnootalogueContext context) { _context = context; } [HttpPost] // [DisableFormValueModelBinding] // [ValidateAntiForgeryToken] public async Task OnPostAsync(UploadDocument ud) { if (!ModelState.IsValid) { return Page(); } if (ud.UploadedFile == null) { return Page(); } var filename = WebUtility.HtmlEncode(ud.UploadedFile.FileName); // var uploadDirectory = Path.Combine(filename); var uploadDirectory = "wwwroot/Content"; var destination = Path.Combine(uploadDirectory, filename); using (var fs = new FileStream(destination, FileMode.Create)) { await ud.UploadedFile.CopyToAsync(fs); } // Document d = _context.Document. Document d = new Document { Title = ud.Title, Authors = ud.Authors.Split(",").ToList(), Category = ud.Category, Tags = string.IsNullOrWhiteSpace(ud.Tags) ? null : ud.Tags.Split(",").ToList(), Notes = ud.Notes, Read = ud.Read, Filename = filename }; _context.Document.Add(d); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException e) { // TODO: handle it like https://github.com/dotnet/AspNetCore.Docs/blob/master/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie30/Pages/Movies/Edit.cshtml.cs#L56 throw e; } return RedirectToPage("/Index"); } } }