管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-09-03 3cfb6aa02516135fb174ab1b30620f2007924663
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
using JiangSu.cs;
using JiangSu.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web;
 
namespace JiangSu.Controllers
{
    public class ImgController : ApiController
    {
        public static string uploadPath = "Images";
 
        public static List<string> extension = new List<string> { ".jpg", "png" };
 
        [HttpGet]
        public List<Img> SelectByPage(string name, int pageSize = 10, int pageIndex = 1)
        {
            try
            {
                return ImgDAL.SelectByPage(name, pageSize, pageIndex);
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                return null;
            }
        }
 
        [HttpGet]
        public Img SelectById(long id)
        {
            try
            {
                return ImgDAL.SelectById(id);
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                return null;
            }
        }
 
        [HttpGet]
        public int DeleteByIds([FromUri] List<int> ids)
        {
            try
            {
                if (null == ids || ids.Count == 0) return 0;
 
                return ImgDAL.DeleteByIds(ids);
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                return 0;
            }
        }
 
        [HttpGet]
        public int DelAll()
        {
            try
            {
                return ImgDAL.DelAll();
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                return 0;
            }
        }
 
        [HttpPost]
        public int Insert([FromBody] Img img)
        {
            try
            {
                if (null == img) return 0;
 
                return ImgDAL.Insert(img);
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                return 0;
            }
        }
 
        [HttpPost]
        public int UpdateById([FromBody] Img img)
        {
            try
            {
                if (null == img) return 0;
 
                return ImgDAL.UpdateById(img);
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                return 0;
            }
        }
 
        [HttpPost]
        public Task<IEnumerable<FileDesc>> Upload()
        {
            string root = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, uploadPath);
            if (!Directory.Exists(root)) Directory.CreateDirectory(root);
 
            if (!Request.Content.IsMimeMultipartContent()) throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
 
            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);
 
                List<FileDesc> infos = new List<FileDesc>();
                foreach (MultipartFileData f in provider.FileData)
                {
                    FileInfo info = new FileInfo(f.LocalFileName);
                    if (!extension.Contains(info.Extension.ToLower()))
                    {
                        File.Delete(f.LocalFileName);
                        continue;
                    }
 
                    string md5 = Tools.CalcFileMD5(f.LocalFileName);
                    string newName = md5 + info.Extension;
                    string newFile = Path.Combine(root, newName);
 
                    List<string> pz = Tools.GetImgGps(f.LocalFileName);
                    Img img = new Img(info.Name, uploadPath + "/" + newName, string.Empty);
                    if (null != pz) img.SetXYZ(pz[0], pz[1], pz[2]);
 
                    FileDesc desc = new FileDesc(info.Name, info.Length);
                    if (File.Exists(newFile))
                        File.Delete(f.LocalFileName);
                    else
                        File.Move(f.LocalFileName, newFile);
 
                    int rows = ImgDAL.Insert(img);
                    if (rows > 0) infos.Add(desc);
                }
 
                return infos;
            });
 
            return task;
        }
 
        [HttpGet]
        public List<string> GetGps()
        {
            return Tools.GetImgGps(@"D:\SEM\病害示例_pos\1.jpg");
        }
    }
}