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 extension = new List { ".jpg", "png" }; [HttpGet] public List 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 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> 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>(t => { if (t.IsFaulted || t.IsCanceled) throw new HttpResponseException(HttpStatusCode.InternalServerError); List infos = new List(); 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 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 GetGps() { return Tools.GetImgGps(@"D:\SEM\病害示例_pos\1.jpg"); } } }