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(); }