管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2023-07-28 015a83929f2f04455c704546f3d3f80d42c23101
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
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)
        {
            //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;
        }
    }
}