// POST: api/qrCode public HttpResponseMessage Post([FromBody] qrCodeViewModel value) { if (value == null) { return(null); } JavaScriptSerializer js = new JavaScriptSerializer(); timeAttRealService service = new timeAttRealService(); HttpResponseMessage response = null; Object result = null; if (value.method == "QRCode") { var resultFile = service.exportFileQRCode(value); HttpResponseMessage resultExport = null; resultExport = Request.CreateResponse(HttpStatusCode.OK); resultExport.Content = new ByteArrayContent(resultFile.FileContents); resultExport.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); resultExport.Content.Headers.ContentDisposition.FileName = resultFile.FileDownloadName; return(resultExport); } string json = js.Serialize(result); response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); return(response); }
public FileContentResult exportFileQRCode(qrCodeViewModel value) { try { if (String.IsNullOrEmpty(value.user_id)) { throw new Exception("Unauthorized Access"); } var userId = JwtHelper.GetUserIdFromToken(value.user_id); if (String.IsNullOrEmpty(userId)) { throw new Exception("Unauthorized Access"); } using (var context = new StandardCanEntities()) { var fileName = "QRCode"; var qrCodeData = String.Format("{0},{1},{2},{3}", value.id, value.req_date, value.start_date, value.stop_date); QRCodeGenerator _qrCode = new QRCodeGenerator(); QRCodeData _qrCodeData = _qrCode.CreateQrCode(qrCodeData, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(_qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(20); var microsoftDateFormatSettings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None, DateFormatHandling = DateFormatHandling.IsoDateFormat, Formatting = Formatting.Indented, }; Bitmap newBitmap; PointF roomLocation = new PointF(70, 30); PointF dateLocation = new PointF(70, 660); PointF topicLocation = new PointF(70, 700); using (Graphics graphics = Graphics.FromImage(qrCodeImage)) { using (Font arialFont = new Font("Arial", 22, FontStyle.Bold)) { graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; graphics.DrawString(value.room_name, arialFont, Brushes.Blue, roomLocation); graphics.DrawString(value.req_date + " " + value.start_date + "-" + value.stop_date, arialFont, Brushes.Blue, dateLocation); graphics.DrawString(value.topic, arialFont, Brushes.Blue, topicLocation); } } newBitmap = new Bitmap(qrCodeImage); byte[] fileBytes = BitmapToBytesCode(newBitmap); FileContentResult result = new FileContentResult(fileBytes, "application/octet-stream"); result.FileDownloadName = fileName; return(result); } } catch (Exception ex) { throw new Exception(ex.Message); } }