Mercurial > Grille > Grille-Config
comparison Controllers/UploadController.cs @ 0:689cde763395
init cimmit
author | Franklin Schmit <meokcin@gmail.com> |
---|---|
date | Thu, 05 Sep 2024 10:16:16 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:689cde763395 |
---|---|
1 using System; | |
2 using System.IO; | |
3 using Microsoft.AspNetCore.Mvc; | |
4 using Microsoft.AspNetCore.Http; | |
5 using Microsoft.AspNetCore.Hosting; | |
6 | |
7 namespace Grille2.Controllers | |
8 { | |
9 public partial class UploadController : Controller | |
10 { | |
11 private readonly IWebHostEnvironment environment; | |
12 | |
13 public UploadController(IWebHostEnvironment environment) | |
14 { | |
15 this.environment = environment; | |
16 } | |
17 | |
18 // Single file upload | |
19 [HttpPost("upload/single")] | |
20 public IActionResult Single(IFormFile file) | |
21 { | |
22 try | |
23 { | |
24 // Put your code here | |
25 return StatusCode(200); | |
26 } | |
27 catch (Exception ex) | |
28 { | |
29 return StatusCode(500, ex.Message); | |
30 } | |
31 } | |
32 | |
33 // Multiple files upload | |
34 [HttpPost("upload/multiple")] | |
35 public IActionResult Multiple(IFormFile[] files) | |
36 { | |
37 try | |
38 { | |
39 // Put your code here | |
40 return StatusCode(200); | |
41 } | |
42 catch (Exception ex) | |
43 { | |
44 return StatusCode(500, ex.Message); | |
45 } | |
46 } | |
47 | |
48 // Multiple files upload with parameter | |
49 [HttpPost("upload/{id}")] | |
50 public IActionResult Post(IFormFile[] files, int id) | |
51 { | |
52 try | |
53 { | |
54 // Put your code here | |
55 return StatusCode(200); | |
56 } | |
57 catch (Exception ex) | |
58 { | |
59 return StatusCode(500, ex.Message); | |
60 } | |
61 } | |
62 | |
63 // Image file upload (used by HtmlEditor components) | |
64 [HttpPost("upload/image")] | |
65 public IActionResult Image(IFormFile file) | |
66 { | |
67 try | |
68 { | |
69 var fileName = $"{Guid.NewGuid()}{Path.GetExtension(file.FileName)}"; | |
70 | |
71 using (var stream = new FileStream(Path.Combine(environment.WebRootPath, fileName), FileMode.Create)) | |
72 { | |
73 // Save the file | |
74 file.CopyTo(stream); | |
75 | |
76 // Return the URL of the file | |
77 var url = Url.Content($"~/{fileName}"); | |
78 | |
79 return Ok(new { Url = url }); | |
80 } | |
81 } | |
82 catch (Exception ex) | |
83 { | |
84 return StatusCode(500, ex.Message); | |
85 } | |
86 } | |
87 } | |
88 } |