-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHomeController.cs
63 lines (57 loc) · 1.83 KB
/
HomeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RichSaveLoadDB.Models;
namespace RichSaveLoadDB.Controllers
{
public class HomeController : Controller
{
private readonly DocsDbContext _context;
public HomeController(DocsDbContext context)
{
_context = context;
_context.Database.EnsureCreated();
}
public IActionResult Index()
{
var model = _context.Docs.FirstOrDefault();
if (model == null)
model = new DocumentInfo();
return View(model);
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
public IActionResult SaveDocument(string base64, string fileName, int format, string reason)
{
byte[] fileContents = System.Convert.FromBase64String(base64);
var doc = _context.Docs.FirstOrDefault();
if (doc == null)
{
doc = new DocumentInfo();
doc.DocumentBytes = fileContents;
doc.DocumentFormat = format;
doc.DocumentName = fileName;
_context.Docs.Add(doc);
}
else
{
doc.DocumentBytes = fileContents;
doc.DocumentFormat = format;
doc.DocumentName = fileName;
}
_context.SaveChanges();
return Ok();
}
}
}