using System; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Web.Http; using System.Web.Http.Results; using WX.CRM.Common; using WxFile.Api.Handle; namespace WxFile.Api.Controllers { public class MessageController : ApiController { private static string _shareName; private static string _isShare; public MessageController() { _isShare = Utility.GetSettingByKey("IsShare"); if (!string.IsNullOrEmpty(_isShare)) { //LogHelper.Info(_isShare); try { _shareName = Utility.GetSettingByKey("NetUseShareName") ?? @"\\192.168.1.171\weixin"; string user = Utility.GetSettingByKey("NetUseUser") ?? @"192.168.1.171\admin"; string pwd = Utility.GetSettingByKey("NetUsePwd") ?? "read,./1"; NetUseHelper.Build(_shareName, user, pwd, string.Empty);//不指定盘符,避免引起盘符被占用的错误 } catch (Exception e) { LogHelper.Error(e.ToString()); } } } /// /// 获取json文件 /// /// /// /// [Route("Message/{jobusername}/{filename}")] [HttpGet] public HttpResponseMessage GetMessage(string jobusername, string filename) { string filepath = _shareName + @"\Message\" + jobusername + "\\" + filename; //LogHelper.Info(filepath); if (!File.Exists(filepath)) { return new HttpResponseMessage { Content = new StringContent("", System.Text.Encoding.UTF8, "application/json") }; } StringBuilder cld = new StringBuilder(); using (StreamReader sr = new StreamReader(filepath)) { string line; // 从文件读取并显示行,直到文件的末尾 while ((line = sr.ReadLine()) != null) { cld.Append(line); } } return new HttpResponseMessage { Content = new StringContent(cld.ToString(), System.Text.Encoding.UTF8, "application/json") }; //return Content(HttpStatusCode.OK, cld.ToString()); } [Route("uploadfile_old/{month}/{filename}")] [HttpGet] public IHttpActionResult GetImg_old(string month, string filename) { string filepath = _shareName + @"\WXFILE\uploadfile\" + month + "\\" + filename; var f = new FileInfo(filepath); if (!f.Exists) { return new StatusCodeResult(HttpStatusCode.NotFound, this); } string contentype = System.Web.MimeMapping.GetMimeMapping(filepath); if (!filename.Contains(".")) { contentype = "image/jpeg"; } return new FileStreamResult(f.OpenRead(), contentype ?? "application/octet-stream"); } [Route("uploadfile2/{month}/{filename}")] [HttpGet] public HttpResponseMessage GetImg2(string month, string filename) { try { string filepath = _shareName + @"\WXFILE\uploadfile\" + month + "\\" + filename; var f = new FileInfo(filepath); if (!f.Exists) { return new HttpResponseMessage(HttpStatusCode.NoContent); } //var stream = new FileStream(filepath, FileMode.Open); //byte[] expectedBytes = new byte[stream.Length]; //HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); //response.Content = new StreamContent(stream); string contentype = System.Web.MimeMapping.GetMimeMapping(filepath); if (!filename.Contains(".")) { contentype = "image/jpeg"; } HttpResponseMessage rsp = this.Request.CreateResponse(HttpStatusCode.OK); byte[] buffer = System.IO.File.ReadAllBytes(filepath); rsp.Content = new StreamContent(new MemoryStream(buffer)); //rsp.Headers.Add("Content-Type", contentype); //rsp.Headers.Add("ContentType", contentype); rsp.Content.Headers.ContentType = new MediaTypeHeaderValue(contentype); LogHelper.Error("end:" + contentype); return rsp; } catch (Exception e) { LogHelper.Error("错误了:" + e.ToString()); return new HttpResponseMessage(HttpStatusCode.NoContent); } } [Route("uploadfile/{month}/{filename}")] [HttpGet] public HttpResponseMessage GetImg(string month, string filename) { try { string filePath = _shareName + @"\WXFILE\uploadfile\" + month + "\\" + filename; var f = new FileInfo(filePath); if (!f.Exists) { return new HttpResponseMessage(HttpStatusCode.NoContent); } //var stream = new FileStream(filepath, FileMode.Open); //byte[] expectedBytes = new byte[stream.Length]; //HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); //response.Content = new StreamContent(stream); string contentype = System.Web.MimeMapping.GetMimeMapping(filePath); if (!filename.Contains(".")) { contentype = "image/jpeg"; } if (contentype.IndexOf("audio") > -1 || contentype.IndexOf("mp4") > -1) { //LogHelper.Error("end:" + contentype); //HttpResponseMessage rsp = this.Request.CreateResponse(HttpStatusCode.OK); HttpResponseMessage rsp = this.Request.CreateResponse(HttpStatusCode.PartialContent); using (StreamReader sr = new StreamReader(filePath)) { long size = 0, start = 0, end = 0; size = sr.BaseStream.Length; end = size - 1; //rsp.Headers.AcceptRanges.Add("0-" + length.ToString()); rsp.Headers.AcceptRanges.Add("bytes"); if (this.Request.Headers.Range != null) { RangeItemHeaderValue requestRange = this.Request.Headers.Range.Ranges.First(); start = requestRange.From.Value; long count = 1024 * 1024 * 5; long anotherEnd = (start + count) > end ? end : (start + count); end = requestRange.To == null ? anotherEnd : requestRange.To.Value; } sr.BaseStream.Seek(start, SeekOrigin.Begin); byte[] buffer = new byte[end - start + 1]; sr.BaseStream.Read(buffer, 0, buffer.Length); MemoryStream ms = new MemoryStream(buffer); rsp.Content = new StreamContent(ms); rsp.Content.Headers.ContentRange = new ContentRangeHeaderValue(start, end, size); rsp.Content.Headers.ContentType = new MediaTypeHeaderValue(contentype); return rsp; } } else { HttpResponseMessage rsp = this.Request.CreateResponse(HttpStatusCode.OK); byte[] buffer = System.IO.File.ReadAllBytes(filePath); rsp.Content = new StreamContent(new MemoryStream(buffer)); rsp.Content.Headers.ContentType = new MediaTypeHeaderValue(contentype); //LogHelper.Error("end:" + contentype); return rsp; } } catch (Exception e) { LogHelper.Error("错误了:" + e.ToString()); return new HttpResponseMessage(HttpStatusCode.NoContent); } } } }