using System; using System.IO; using System.Linq; using System.ServiceModel; using System.ServiceModel.Web; using WX.CRM.Common; namespace WxService { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WeiXinService”。 // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 WeiXinService.svc 或 WeiXinService.svc.cs,然后开始调试。 public class WeiXinService : IWeiXinService { ValidationErrors errors = new ValidationErrors(); public bool UploadQrCode(Stream input, string filePath) { if (!input.CanRead || string.IsNullOrWhiteSpace(filePath)) { //return new JsonResult { result = false, retcode = (int)EnumInterfaceErrcode.参数错误, retmsg = "参数错误" }; //throw new Exception("数据流不可读!"); return false; } try { var allowFile = new string[] { ".jpg", ".bmp", ".gif", ".jpeg", ".png" }; string extName = filePath.Substring(filePath.LastIndexOf('.')).ToLower(); if (!allowFile.Any(m => m == extName)) { return false; } string uploadFolder = AppDomain.CurrentDomain.BaseDirectory; string fileDir = filePath.Substring(0, filePath.LastIndexOf('/')); FileStream targetStream = null; uploadFolder = uploadFolder + fileDir; if (!Directory.Exists(uploadFolder)) { Directory.CreateDirectory(uploadFolder); } string fileFullPath = AppDomain.CurrentDomain.BaseDirectory + filePath; using (targetStream = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write, FileShare.None)) { const int bufferLen = 4096; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = input.Read(buffer, 0, bufferLen)) > 0) { targetStream.Write(buffer, 0, count); } targetStream.Close(); input.Close(); } return true; //return new JsonResult { result = true, retcode = (int)EnumInterfaceErrcode.调用成功, retmsg = filePath }; } catch (Exception ex) { LogHelper.Error("上传二维码到接口站点错误:" + ex.Message); //throw new Exception("上传二维码到接口站点错误"); return false; //return new JsonResult { result = false, retcode = (int)EnumInterfaceErrcode.系统错误, retmsg = null }; } } } [ServiceContract] public interface IWeiXinService { [OperationContract] [WebInvoke(Method = "*", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "UploadQrCode?filePath={filePath}")] bool UploadQrCode(Stream input, string filePath); } }