mostly working uploads 0u0

This commit is contained in:
Lynne Megido 2020-09-18 15:35:20 +10:00
parent c4cb226330
commit ed820f60fc
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
8 changed files with 173 additions and 11 deletions

View File

@ -3,7 +3,7 @@
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
ViewData["Title"] = "Edit";
ViewData["Title"] = "Edit";
}
<form method="POST">
@ -15,14 +15,23 @@
<input asp-for="ed.Title" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="ed.Authors" class="control-label"></label>
<input asp-for="ed.Authors" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="ed.Category" class="control-label"></label>
<input asp-for="ed.Category" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="ed.Tags" class="control-label"></label>
<input asp-for="ed.Tags" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="ed.Notes" class="control-label"></label>
@* <input asp-for="EditDocument.Notes" class="form-control"/> *@
@Html.TextAreaFor(model => model.ed.Notes)
</div>

View File

@ -31,9 +31,9 @@ namespace Snootalogue.Pages.Documents {
ed = new EditDocument {
ID = Document.ID,
Title = Document.Title,
Authors = Document.Authors,
Authors = string.Join(",", Document.Authors),
Category = Document.Category,
Tags = Document.Tags,
Tags = Document.Tags == null ? "" : string.Join(",", Document.Tags),
Read = Document.Read
};
@ -47,7 +47,9 @@ namespace Snootalogue.Pages.Documents {
Document d = _context.Document.First(d => d.ID == ed.ID);
d.Title = ed.Title;
d.Authors = ed.Authors.Split(",").ToList<string>();
d.Category = ed.Category;
d.Tags = string.IsNullOrWhiteSpace(ed.Tags) ? null : ed.Tags.Split(",").ToList<string>();
d.Notes = ed.Notes;
d.Read = ed.Read;

View File

@ -16,7 +16,7 @@
<div class="authors">By @Html.DisplayFor(modelItem => item.Authors)</div>
<div class="category">Category: @Html.DisplayFor(modelItem => item.Category)</div>
<div class="tags">@Html.DisplayFor(modelItem => item.Tags)</div>
@{string hash = item.Hash.Substring(0, 8);}
@{string hash = item.Hash?.Substring(0, 8);}
<div class="metadata">
@Html.DisplayFor(modelItem => item.Filename) |
<span title="@item.Size bytes">@Html.DisplayFor(modelItem => item.Size)</span> |

View File

@ -1,4 +1,5 @@
<!DOCTYPE html>
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html lang="en">
<head>
@ -14,7 +15,7 @@
<div id="nav-controls">
<input id="nav-search" type="search" placeholder="Search...">
<a class="button inverted" href="#">Help</a>
<a class="button inverted" href="#">Upload</a>
<a class="button inverted" asp-page="/Upload">Upload</a>
</div>
</nav>

55
Pages/Upload.cshtml Normal file
View File

@ -0,0 +1,55 @@
@page
@model Snootalogue.Pages.UploadModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
ViewData["Title"] = "Upload";
}
<form asp-action="Upload" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ud.Title" class="control-label"></label>
<input asp-for="ud.Title" class="form-control" placeholder="Determine from file"/>
</div>
<div class="form-group">
<label asp-for="ud.Authors" class="control-label"></label>
<input asp-for="ud.Authors" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="ud.Category" class="control-label"></label>
<input asp-for="ud.Category" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="ud.Tags" class="control-label"></label>
<input asp-for="ud.Tags" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="ud.Notes" class="control-label"></label>
@Html.TextAreaFor(model => model.ud.Notes)
</div>
<div class="form-group">
<label asp-for="ud.Read" class="control-label"></label>
<input asp-for="ud.Read" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="ud.UploadedFile" class="control-label"></label>
<input asp-for="ud.UploadedFile" class="form-control" accept=".pdf,application/pdf"/>
</div>
<div class="centred">
<input type="submit" class="button block" value="Upload"/>
<br>
<a href="/" class="button block">Cancel</a>
</div>
</form>
@* value="@Model.EditDocument.Title" *@

69
Pages/Upload.cshtml.cs Normal file
View File

@ -0,0 +1,69 @@
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<IActionResult> 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<string>(),
Category = ud.Category,
Tags = string.IsNullOrWhiteSpace(ud.Tags) ? null : ud.Tags.Split(",").ToList<string>(),
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");
}
}
}

View File

@ -10,11 +10,13 @@ namespace Snootalogue.ViewModels {
public int ID { get; set; }
[Required]
public string Title { get; set; }
[UIHint("CommaSeparatedList")]
public List<string> Authors { get; set; }
[Required]
[Display(Name = "Authors (comma separated)")]
public string Authors { get; set; }
[Required]
public string Category { get; set; }
[UIHint("CommaSeparatedList")]
public List<string> Tags { get; set; }
[Display(Name = "Tags (comma separated)")]
public string Tags { get; set; }
public Boolean Read { get; set; }
public string Notes { get; set; }
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http;
// a version of the Document model designed for user editing
namespace Snootalogue.ViewModels {
public class UploadDocument {
[Required]
public string Title { get; set; }
[Required]
[Display(Name = "Authors (comma separated)")]
public string Authors { get; set; }
[Required]
public string Category { get; set; }
[Display(Name = "Tags (comma separated)")]
public string Tags { get; set; }
public Boolean Read { get; set; }
public string Notes { get; set; }
[Display(Name = "File to upload")]
public IFormFile UploadedFile { get; set; }
}
}