username@email.com
2024-12-26 90858c80d9921b555119f41060c1f883f6e6ffc5
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using Microsoft.AspNetCore.Mvc;
 
namespace DocumentFile.Service.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DocumentController : ControllerBase
    {
        private readonly IUploadService _uploadService;
 
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="uploadService"></param>
        public DocumentController(IUploadService uploadService)
        {
            _uploadService = uploadService;
        }
 
        ///// <summary>
        ///// 上传文件模板(doc、docx)
        ///// </summary>
        ///// <param name="code">租户唯一标识code</param>
        ///// <param name="formFile">文件</param>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<IActionResult> UploadDocTemplate([FromForm]string code,IFormFile formFile)
        //{
        //    ReturnMsg msg = new ReturnMsg();
        //    if(formFile.Length>0)
        //    {
        //        msg = await this._uploadService.HandleUploadWordFile(formFile,code,"");                
        //    }
        //    else
        //    {
        //        msg.error = "请上传文件";
        //    }
 
        //    return new JsonResult(msg);
        //}
 
        /// <summary>
        /// 上传文件(doc、docx)
        /// 本地指定路径
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadDocWithPath([FromForm]string code, [FromForm] string path, IFormFile formFile)
        {
            ReturnMsg msg = new ReturnMsg();
            if (formFile.Length > 0)
            {
                msg = await this._uploadService.HandleUploadWordFile(formFile,code,path);
            }
            else
            {
                msg.error = "请上传文件";
            }
 
            return new JsonResult(msg);
        }
 
        /// <summary>
        /// 上传文件(doc、docx)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadDocInstance([FromForm] string code, IFormFile formFile)
        {
            ReturnMsg msg = new ReturnMsg();
            if (formFile.Length > 0)
            {
                msg = await this._uploadService.HandleUploadWordFile(formFile, code, "");
            }
            else
            {
                msg.error = "请上传文件";
            }
 
            return new JsonResult(msg);
        }
 
        /// <summary>
        /// 上传图片(jpg、jpeg、png)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadPhoto([FromForm] string code, IFormFile formFile)
        {
            ReturnMsg msg = new ReturnMsg();
            if (formFile.Length > 0)
            {
                msg = await this._uploadService.HandleUploadImageFile(formFile, code);
            }
            else
            {
                msg.error = "请上传文件";
            }
 
            return new JsonResult(msg);
        }
 
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="delurl"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteFile([FromForm] string[] delurl)
        {
            ReturnMsg msg = new ReturnMsg();
            try
            {
                if(delurl!=null && delurl.Length > 0)
                {
                    var dir = AppDomain.CurrentDomain.BaseDirectory;
                    foreach (var item in delurl)
                    {
                        if (!string.IsNullOrEmpty(item))
                        {
                            var path = dir + item;
                            if (System.IO.File.Exists(path))
                            {
                                System.IO.File.Delete(path);
                            }
                        }
                    }
                    msg.code = 1;
                }                
            }
            catch (Exception ex)
            {
                NLogProvider.GetInstance().Error(ex);
            }
            return new JsonResult(msg);
        }
 
 
 
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="delurl"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult FileLastWriteTime([FromForm] string delurl)
        {
            ReturnMsg msg = new ReturnMsg();
            try
            {
                if (delurl != null && delurl.Length > 0)
                {
                    var dir = AppDomain.CurrentDomain.BaseDirectory;
 
                    if (!string.IsNullOrEmpty(delurl))
                    {
                        var path = dir + delurl;
                        if (System.IO.File.Exists(path))
                        {
                            var lastWriteTime = System.IO.File.GetLastWriteTime(path);
                            msg.lastWriteTime = lastWriteTime.ToString();
                        }
                    }
                    msg.code = 1;
                }
            }
            catch (Exception ex)
            {
                NLogProvider.GetInstance().Error(ex);
            }
            return new JsonResult(msg);
        }
 
 
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Download([FromForm] string url)
        {
            try
            {
                if (!string.IsNullOrEmpty(url))
                {
                    var _webRootPath = AppDomain.CurrentDomain.BaseDirectory;
                    var path = _webRootPath + url;
                    if (System.IO.File.Exists(path))
                    {
                        return new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream");// { FileDownloadName = FileName };
                    }
                }
            }
            catch (Exception ex)
            {
                NLogProvider.GetInstance().Error(ex);                
            }
            return BadRequest();
        }
    }
}