JiangSu/App_Start/WebApiConfig.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
JiangSu/Controllers/ImgController.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
JiangSu/JiangSu.csproj | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
JiangSu/Models/FileDesc.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
JiangSu/cs/CustomMultipartFormDataStreamProvider.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
JiangSu/cs/NoBufferPolicySelector.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
JiangSu/cs/Tools.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
JiangSu/index.html | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
JiangSu/App_Start/WebApiConfig.cs
@@ -1,8 +1,10 @@ using Newtonsoft.Json.Converters; using JiangSu.cs; using Newtonsoft.Json.Converters; using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; using System.Web.Http.Hosting; namespace JiangSu { @@ -17,6 +19,9 @@ defaults: new { id = RouteParameter.Optional } ); // æ³¨åæ©å±ä¸»æºç¼åPolicy GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferPolicySelector()); // å±è½è¿åXMLæ ¼å¼ GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); 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"); } } } JiangSu/JiangSu.csproj
@@ -142,17 +142,20 @@ <Compile Include="Controllers\OpController.cs" /> <Compile Include="Controllers\ValuesController.cs" /> <Compile Include="Controllers\GridController.cs" /> <Compile Include="cs\CustomMultipartFormDataStreamProvider.cs" /> <Compile Include="cs\GridDAL.cs" /> <Compile Include="cs\ImgDAL.cs" /> <Compile Include="cs\LogOut.cs" /> <Compile Include="cs\ModelDAL.cs" /> <Compile Include="cs\ModelHandler.cs" /> <Compile Include="cs\NoBufferPolicySelector.cs" /> <Compile Include="cs\PostgreHelper.cs" /> <Compile Include="cs\SQLiteHelper.cs" /> <Compile Include="cs\Tools.cs" /> <Compile Include="Global.asax.cs"> <DependentUpon>Global.asax</DependentUpon> </Compile> <Compile Include="Models\FileDesc.cs" /> <Compile Include="Models\Grid.cs" /> <Compile Include="Models\Img.cs" /> <Compile Include="Models\Model.cs" /> JiangSu/Models/FileDesc.cs
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,24 @@ using JiangSu.cs; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace JiangSu.Models { /// <summary> /// æä»¶æè¿° /// </summary> public class FileDesc { public FileDesc(string name, long length) { this.Name = name; this.Length = Tools.FormatBytes(length); } public string Name { set; get; } public string Length { set; get; } } } JiangSu/cs/CustomMultipartFormDataStreamProvider.cs
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,23 @@ using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Web; namespace JiangSu.cs { public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider { public CustomMultipartFormDataStreamProvider(string path) : base(path) { } public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers) { //string name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? headers.ContentDisposition.FileName : "NoName"; //return name.Replace("\"", string.Empty); return headers.ContentDisposition.FileName.Replace("\"", string.Empty); } } } JiangSu/cs/NoBufferPolicySelector.cs
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,27 @@ using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Web; using System.Web.Http.WebHost; namespace JiangSu.cs { public class NoBufferPolicySelector : WebHostBufferPolicySelector { public override bool UseBufferedInputStream(object hostContext) { var context = hostContext as HttpContextBase; if (context != null) { if (context.Request.HttpMethod == HttpMethod.Post.ToString() && context.Request.ContentLength > 102400) { return false; } } return true; } } } JiangSu/cs/Tools.cs
@@ -3,8 +3,12 @@ using System.Collections.Generic; using System.Data.Common; using System.Data.SQLite; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Web; namespace JiangSu.cs @@ -92,5 +96,105 @@ return list.ToArray(); } /// <summary> /// åèæ ¼å¼å /// </summary> public static string FormatBytes(long bytes) { string[] Suffix = { "Byte", "KB", "MB", "GB", "TB" }; int i = 0; double dblSByte = bytes; if (bytes > 1024) for (i = 0; (bytes / 1024) > 0; i++, bytes /= 1024) dblSByte = bytes / 1024.0; return String.Format("{0:0.##}{1}", dblSByte, Suffix[i]); } /// <summary> /// è®¡ç®æä»¶çMD5ç /// </summary> public static string CalcFileMD5(string filePath) { using (var md5 = new MD5CryptoServiceProvider()) { using (var stream = File.OpenRead(filePath)) { byte[] hash = md5.ComputeHash(stream); return BitConverter.ToString(hash).Replace("-", "").ToLower(); } } } /// <summary> /// è·åå¾ççGPSä¿¡æ¯ /// </summary> public static string GetImgGps(string path) { Image img = null; try { img = Image.FromFile(path); var items = img.PropertyItems.OrderByDescending(x => x.Id); List<string> list = new List<string> { "0", "0", "0" }; foreach (PropertyItem item in items) { if (item.Id >= 0x0000 && item.Id <= 0x001e) // åªåIdèå´ä¸º0x0000å°0x001e { switch (item.Id) { case 0x0002: // 设置纬度 list[1] = GetGpsVal(item.Value); break; case 0x0004: // 设置ç»åº¦ list[0] = GetGpsVal(item.Value); break; case 0x0006: if (item.Value.Length == 8) { double altitude = BitConverter.ToUInt32(item.Value, 0) * 1.0d / BitConverter.ToUInt32(item.Value, 4); list[2] = altitude.ToString("0.000"); } break; } } } return string.Format("POINT Z ({0})", string.Join(" ", list.ToArray())); } catch (Exception ex) { LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); return null; } finally { if (null != img) img.Dispose(); } } /// <summary> /// è·åGPSå¼ /// </summary> private static string GetGpsVal(byte[] bytes) { if (bytes.Length != 24) return "0"; //degrees(å°byte[0]~byte[3]转æuint, é¤ä»¥byte[4]~byte[7]转æçuint) double d = BitConverter.ToUInt32(bytes, 0) * 1.0d / BitConverter.ToUInt32(bytes, 4); //minutes(å°byte[8]~byte[11]转æuint, é¤ä»¥byte[12]~byte[15]转æçuint) double m = BitConverter.ToUInt32(bytes, 8) * 1.0d / BitConverter.ToUInt32(bytes, 12); //seconds(å°byte[16]~byte[19]转æuint, é¤ä»¥byte[20]~byte[23]转æçuint) double s = BitConverter.ToUInt32(bytes, 16) * 1.0d / BitConverter.ToUInt32(bytes, 20); double val = (((s / 60 + m) / 60) + d); return val.ToString("0.00000000"); } } } JiangSu/index.html
@@ -116,7 +116,10 @@ </script> </head> <body> <a href="http://localhost/JiangSu/Values/Get">Test</a> <br /> <br /> <a href="http://localhost/JiangSu/Values/Get">Test</a> <span style="width: 50px;"> </span> <a href="http://localhost/JiangSu/Img/GetGps">GetGps</a> <br /> <br /> <a href="http://localhost/JiangSu/Op/SelectByPage?pageSize=10&pageIndex=1&name=">SelectByPage?pageSize=10&pageIndex=1&name=</a> <br />