public ComResult Print(PrintModel model) { var xpsStream = this.MergeSvc.Merge(model); if (model.Action == PrintActionType.File || model.Action == PrintActionType.PrintAndFile) { var dirPath = $"{this.HostEnv.ContentRootPath}\\wwwroot\\download\\{DateTime.Now.ToString("yyyyMMdd")}"; if (!System.IO.Directory.Exists(dirPath)) { System.IO.Directory.CreateDirectory(dirPath); } var fileName = $"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xps"; var filePath = $"{dirPath}\\{fileName}"; using (var fileStream = new FileStream(filePath, FileMode.Create)) { xpsStream.WriteTo(fileStream); fileStream.Close(); } if (model.Action == PrintActionType.PrintAndFile) { for (int i = 0; i < model.Copies; i++) { XpsPrintHelper.Print(xpsStream, model.PrintName, Guid.NewGuid().ToString("N"), model.IsWait); } } var url = filePath.Replace($"{this.HostEnv.ContentRootPath}\\wwwroot", "").Replace("\\", "/"); return(new ComResult() { Code = "0", Msg = "操作成功", Data = url, Success = true }); } else { for (int i = 0; i < model.Copies; i++) { XpsPrintHelper.Print(xpsStream, model.PrintName, Guid.NewGuid().ToString("N"), model.IsWait); } return(new ComResult() { Code = "0", Msg = "操作成功", Data = model.ToJson(), Success = true }); } }
public ActionResult <TestModel> Get() { var testXps = $"{this.HostEnv.ContentRootPath}\\wwwroot\\Test.xps"; if (System.IO.File.Exists(testXps)) { var printerName = "KONICA MINOLTA bizhub C226 PCL (10.76.20.30) UPD"; XpsPrintHelper.Print(testXps, printerName, DateTime.Now.ToString(), false); return(new TestModel() { Id = 1, Name = printerName, CreateTime = DateTime.Now }); } else { return(NotFound()); } }
private void RequextAction(HttpListenerContext context) { var request = context.Request; var response = context.Response; try { this.Log.Info(request.Url); var model = this.GetPrintModel(request); var dicJson = model.ToJson(); this.Log.Info(dicJson); var printDoc = new MergeDocument(model); var xpsStream = printDoc.MergeToStream(); if (model.Action == PrintActionType.Print) { for (int i = 0; i < model.Copies; i++) { xpsStream.Seek(0, SeekOrigin.Begin); XpsPrintHelper.Print(xpsStream, model.PrintName, $"XPS_{i}_{DateTime.Now.ToString("yyMMddHHmmssfff")}", false); } var resBytes = Encoding.UTF8.GetBytes(dicJson); response.ContentType = "application/json"; response.StatusCode = 200; response.ContentLength64 = resBytes.Length; response.ContentEncoding = Encoding.UTF8; response.OutputStream.Write(resBytes, 0, resBytes.Length); } if (model.Action == PrintActionType.File) { response.ContentType = "application/vnd.ms-xpsdocument"; response.AddHeader("Content-Disposition", $"attachment; filename=XPS_{DateTime.Now.ToString("yyMMddHHmmssfff")}.xps"); response.StatusCode = 200; response.ContentLength64 = xpsStream.Length; //response.ContentEncoding = Encoding.UTF8; xpsStream.Seek(0, SeekOrigin.Begin); xpsStream.CopyTo(response.OutputStream); } if (model.Action == PrintActionType.PrintAndFile) { for (int i = 0; i < model.Copies; i++) { xpsStream.Seek(0, SeekOrigin.Begin); XpsPrintHelper.Print(xpsStream, model.PrintName, $"XPS_{i}_{DateTime.Now.ToString("yyMMddHHmmssfff")}", false); } response.ContentType = "application/vnd.ms-xpsdocument"; response.AddHeader("Content-Disposition", $"attachment; filename=XPS_{DateTime.Now.ToString("yyMMddHHmmssfff")}.xps"); response.StatusCode = 200; response.ContentLength64 = xpsStream.Length; //response.ContentEncoding = Encoding.UTF8; xpsStream.Seek(0, SeekOrigin.Begin); xpsStream.CopyTo(response.OutputStream); } xpsStream.Dispose(); printDoc.Dispose(); } catch (Exception ex) { this.Log.Error(ex.Message, ex); var json = ex.ToJson(); var bytes = Encoding.UTF8.GetBytes(json); response.ContentType = "application/json"; response.StatusCode = 500; response.ContentLength64 = bytes.Length; response.ContentEncoding = Encoding.UTF8; response.OutputStream.Write(bytes, 0, bytes.Length); } response.OutputStream.Flush(); response.OutputStream.Close(); }