管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-09-07 8d7a67ab1d635cb954337d8a767878ae526dd3dc
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
using ExportMap.cs;
using ExportMap.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
 
namespace ExportMap.Controllers
{
    public class UploadController : ApiController
    {
        [HttpPost]
        public Task<IEnumerable<FileDesc>> Post([FromUri] string path, [FromUri] string token)
        {
            if (string.IsNullOrEmpty(token) || !ExportUtil.VerifyToken(token))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized, "令牌为空或无效!"));
            }
 
            //string root = HttpContext.Current.Server.MapPath("~/uploads");
            if (string.IsNullOrEmpty(path)) return null;
            string root = Path.Combine(Tools.GetSetting("tempFolder"), path);
            if (!Directory.Exists(root)) Directory.CreateDirectory(root);
 
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "请求的格式不正确!"));
            }
 
            var provider = new CustomMultipartFormDataStreamProvider(root);
            var task = Request.Content.ReadAsMultipartAsync(provider).ContinueWith<IEnumerable<FileDesc>>(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }
 
                var fileInfos = provider.FileData.Select(i =>
                {
                    FileInfo info = new FileInfo(i.LocalFileName);
 
                    return new FileDesc(info.Name, info.Length);
                });
 
                return fileInfos;
            });
 
            return task;
        }
    }
}