管道基础大数据平台系统开发-【CS】-ExportMap
OK
13693261870
2024-09-27 566e0d21293a5fe6423fd7a16541bce00eeb2e38
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)
        {
@@ -55,6 +63,20 @@
            }
        }
        [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)
        {
@@ -86,5 +108,58 @@
                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");
        }
    }
}