-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileController.cs
50 lines (42 loc) · 1.38 KB
/
FileController.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
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using FileStorageApp.Models;
using System.IO;
using System.Threading.Tasks;
namespace FileStorageApp.Controllers
{
public class FileController : Controller
{
private readonly IWebHostEnvironment _environment;
public FileController(IWebHostEnvironment environment)
{
_environment = environment;
}
public IActionResult Index()
{
var files = Directory.GetFiles(Path.Combine(_environment.WebRootPath, "uploads"));
ViewBag.Files = files;
return View();
}
public IActionResult Upload()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Upload(FileUploadViewModel model)
{
if (ModelState.IsValid)
{
var uploads = Path.Combine(_environment.WebRootPath, "uploads");
var filePath = Path.Combine(uploads, model.File.FileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await model.File.CopyToAsync(fileStream);
}
return RedirectToAction("Index");
}
return View(model);
}
}
}