qwj
2023-07-31 b0d124029b427ead63bb6c6517d911b65fff3f7a
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
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
 
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>
        /// 上传文件(word)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadDocument(IFormFile formFile)
        {
            ReturnMsg msg = new ReturnMsg();
            if(formFile.Length>0)
            {
                msg = await this._uploadService.HandleUploadWordFile(formFile);                
            }
            else
            {
                msg.error = "请上传文件";
            }
 
            return new JsonResult(msg);
        }
 
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Download([FromBody] string url)
        {
            try
            {
                if (!string.IsNullOrEmpty(url))
                {
                    var _webRootPath = AppDomain.CurrentDomain.BaseDirectory;
 
                    return new FileStreamResult(new FileStream(_webRootPath + url, FileMode.Open), "application/octet-stream");// { FileDownloadName = FileName };
                }
            }
            catch (Exception ex)
            {
                NLogProvider.GetInstance().Error(ex);                
            }
            return BadRequest();
        }
    }
}