From 15eabfdabf30973c1e99b015060b7da2190ccdf1 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期六, 06 一月 2024 15:47:26 +0800
Subject: [PATCH] 获取图片的GPS信息

---
 JiangSu/Controllers/ImgController.cs |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/JiangSu/Controllers/ImgController.cs b/JiangSu/Controllers/ImgController.cs
index a2e0a22..a6da6c5 100644
--- a/JiangSu/Controllers/ImgController.cs
+++ b/JiangSu/Controllers/ImgController.cs
@@ -2,15 +2,23 @@
 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)
         {
@@ -86,5 +94,55 @@
                 return 0;
             }
         }
+
+        [HttpPost]
+        public Task<IEnumerable<FileDesc>> Post()
+        {
+            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, uploadPath, newName);
+
+                    Img img = new Img();
+                    img.name = info.Name;
+                    img.path = uploadPath + "\\" + newName;
+                    img.geom = Tools.GetImgGps(f.LocalFileName);
+
+                    if (!File.Exists(newFile)) File.Move(f.LocalFileName, newFile);
+                    int rows = ImgDAL.Insert(img);
+
+                    infos.Add(new FileDesc(info.Name, info.Length));
+                }
+
+                return infos;
+            });
+
+            return task;
+        }
+
+        [HttpGet]
+        public string GetGps()
+        {
+            return Tools.GetImgGps(@"D:\SEM\鐥呭绀轰緥_pos\1.jpg");
+        }
     }
 }

--
Gitblit v1.9.3