markus / ConvertService / ServiceBase / FileDownloadSite / Controllers / HomeController.cs @ 53c9637d
이력 | 보기 | 이력해설 | 다운로드 (1.9 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.IO; |
4 |
using System.Linq; |
5 |
using System.Threading.Tasks; |
6 |
using FileDownloadSite.Models.Home; |
7 |
using Microsoft.AspNetCore.Mvc; |
8 |
using Microsoft.Extensions.FileProviders; |
9 |
using MimeKit; |
10 |
|
11 |
namespace FileDownloadSite.Controllers |
12 |
{ |
13 |
public class HomeController : Controller |
14 |
{ |
15 |
private readonly IFileProvider fileProvider; |
16 |
|
17 |
public HomeController(IFileProvider fileProvider) |
18 |
{ |
19 |
this.fileProvider = fileProvider; |
20 |
} |
21 |
|
22 |
public IActionResult Index() |
23 |
{ |
24 |
return View(); |
25 |
} |
26 |
|
27 |
/// <summary> |
28 |
/// 파일 폴더의 파일 리스트 |
29 |
/// </summary> |
30 |
/// <param name="FileName"></param> |
31 |
/// <returns></returns> |
32 |
public IActionResult GetFileList() |
33 |
{ |
34 |
var model = new FilesViewModel(); |
35 |
|
36 |
foreach (var item in this.fileProvider.GetDirectoryContents("")) |
37 |
{ |
38 |
model.Files.Add( |
39 |
new FileDetails { Name = item.Name, Path = item.PhysicalPath }); |
40 |
} |
41 |
return Ok(model.Files); |
42 |
} |
43 |
|
44 |
/// <summary> |
45 |
// 지정된 파일을 가져온다. |
46 |
/// </summary> |
47 |
/// <param name="FileName"></param> |
48 |
/// <returns></returns> |
49 |
public async Task<IActionResult> GetFile(string FileName) |
50 |
{ |
51 |
var file = this.fileProvider.GetFileInfo(FileName); |
52 |
|
53 |
if (file.Exists) |
54 |
{ |
55 |
var memory = new MemoryStream(); |
56 |
using (var stream = file.CreateReadStream()) |
57 |
{ |
58 |
await stream.CopyToAsync(memory); |
59 |
} |
60 |
memory.Position = 0; |
61 |
return File(memory, MimeKit.MimeTypes.GetMimeType(FileName), Path.GetFileName(FileName)); |
62 |
} |
63 |
else |
64 |
{ |
65 |
return new NotFoundResult(); |
66 |
} |
67 |
} |
68 |
} |
69 |
} |