| | |
| | | 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) |
| | | { |
| | |
| | | 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"); |
| | | } |
| | | } |
| | | } |