public async Task <HttpResponseMessage> PrintAssessmentPDF(ComprehensiveAssessmentRequest comprehensiveAssessmentRequest) { try { httpResponseMessage = new HttpResponseMessage(); comprehensiveAssessmentPDFResponse = new ComprehensiveAssessmentPDFResponse(); if (ModelState.IsValid && comprehensiveAssessmentRequest != null) { if (Request.Headers.Contains("Source")) { companyId = Request.Headers.GetValues("Source").First(); } comprehensiveAssessmentPDFResponse = await _ComprehensiveAssessmentService.PrintAssessmentPDF(comprehensiveAssessmentRequest, companyId); httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, comprehensiveAssessmentPDFResponse); Stream stream = CommonFunctions.GetFilesStream(comprehensiveAssessmentPDFResponse.ComprehensiveAssessmentPDF[0].FileName); httpResponseMessage.Content = new StreamContent(stream); httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); httpResponseMessage.Content.Headers.ContentDisposition.FileName = "fileNameOfYourChoice"; } } catch (Exception Ex) { cadResponse.Success = false; cadResponse.IsException = true; cadResponse.Message = Ex.Message; httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, cadResponse); CommonFunctions.LogError(Ex); } return(httpResponseMessage); }
public async Task <HttpResponseMessage> UploadOfflinePDF() { try { if (Request.Headers.Contains("Source")) { companyId = Request.Headers.GetValues("Source").First(); } httpResponseMessage = new HttpResponseMessage(); comprehensiveAssessmentPDFResponse = new ComprehensiveAssessmentPDFResponse(); string fileName = string.Empty; if (Request.Content.IsMimeMultipartContent()) { var uloadPath = ConfigurationManager.AppSettings["UploadOfflinePDF"]; int iUploadedCnt = 0; System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files; // CHECK THE FILE COUNT. for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++) { System.Web.HttpPostedFile hpf = hfc[iCnt]; if (hpf.ContentLength > 0) { int lastIndex = hpf.FileName.LastIndexOf("."); fileName = hpf.FileName.Substring(0, lastIndex); DateTime currentUTC = DateTime.UtcNow; string strTemp = currentUTC.ToString("yyyyMMddHHmmss"); fileName = fileName + "_" + strTemp + ".pdf"; // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE) if (!File.Exists(uloadPath + Path.GetFileName(fileName))) { // SAVE THE FILES IN THE FOLDER. hpf.SaveAs(uloadPath + Path.GetFileName(fileName)); } } } Dictionary <string, string> attributes = new Dictionary <string, string>(); Dictionary <string, byte[]> files = new Dictionary <string, byte[]>(); var provider = new MultipartMemoryStreamProvider(); await Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.Contents) { if (file.Headers.ContentDisposition.FileName == null) { foreach (NameValueHeaderValue p in file.Headers.ContentDisposition.Parameters) { string clientId = p.Value; if (clientId.StartsWith("\"") && clientId.EndsWith("\"")) { clientId = clientId.Substring(1, clientId.Length - 2); } string value = await file.ReadAsStringAsync(); attributes.Add(clientId, value); } } } attributes.Add("PDFDocument", uloadPath + fileName); var json = JsonConvert.SerializeObject(attributes); var pdfFile = JsonConvert.SerializeObject(files); var data = await _ComprehensiveAssessmentService.UploadOfflinePDF(json, pdfFile, companyId); httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, data); } } catch (Exception Ex) { cadResponse.Success = false; cadResponse.IsException = true; cadResponse.Message = Ex.Message; httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, cadResponse); CommonFunctions.LogError(Ex); } return(httpResponseMessage); }