1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . IO ;
4
+ using System . Linq ;
5
+ using System . Threading . Tasks ;
6
+ using Microsoft . AspNetCore . Http ;
7
+ using Microsoft . AspNetCore . Mvc ;
8
+ using Newtonsoft . Json . Linq ;
9
+ using SixLabors . ImageSharp ;
10
+ using SixLabors . ImageSharp . Processing ;
11
+
12
+ namespace HelperSrv_2 . Controllers
13
+ {
14
+ [ Produces ( "application/json" ) ]
15
+ [ Route ( "api/ImgUpload" ) ]
16
+ public class ImgUploadController : Controller
17
+ {
18
+ //懒得弄到依赖注入里面了,嘤嘤嘤
19
+ private static Dictionary < string , string > pendingAvater = new Dictionary < string , string > ( ) ;
20
+ [ HttpPost ( "Upload" ) ]
21
+ public string Upload ( [ FromForm ] string UserName , [ FromForm ] IFormFile userAvater )
22
+ {
23
+ try
24
+ {
25
+ using ( var stream = userAvater . OpenReadStream ( ) )
26
+ {
27
+ string guid = Guid . NewGuid ( ) . ToString ( ) + ".png" ;
28
+ string dstPath = Path . Combine ( "Images" , guid ) ;
29
+ using ( var img = Image . Load ( stream ) )
30
+ {
31
+ img . Mutate ( x => x . Resize ( 96 , 96 ) ) ;
32
+ img . Save ( Path . Combine ( dstPath ) ) ;
33
+ }
34
+ pendingAvater . Add ( UserName , dstPath ) ;
35
+ }
36
+ }
37
+ catch ( Exception e )
38
+ {
39
+ return ( new JObject { { "status" , "1" } , { "error" , e . ToString ( ) } } ) . ToString ( ) ;
40
+ }
41
+ return ( new JObject { { "status" , "0" } } ) . ToString ( ) ;
42
+ }
43
+
44
+ [ HttpGet ( "List" ) ]
45
+ public string List ( )
46
+ {
47
+ var obj = new JObject ( ) ;
48
+ JArray datas = new JArray ( ) ;
49
+ foreach ( var entry in pendingAvater )
50
+ {
51
+ datas . Add ( new JObject
52
+ {
53
+ { "name" , entry . Key } ,
54
+ { "img" , entry . Value }
55
+ } ) ;
56
+ }
57
+ obj . Add ( "datas" , datas ) ;
58
+ return obj . ToString ( ) ;
59
+ }
60
+ }
61
+ }
0 commit comments