public async Task <HttpResponseMessage> DownloadUPloadedFile(IncidentManagementRequest incidentManagementRequest) { try { common = new CommonFunctions(); httpResponseMessage = new HttpResponseMessage(); allPDFUploadResponse = new AllPDFUploadResponse(); if (ModelState.IsValid && incidentManagementRequest != null) { allPDFUploadResponse = await _IIncidentManagementService.DownloadUPloadedFile(incidentManagementRequest); httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, allPDFUploadResponse); Stream stream = CommonFunctions.GetFilesStream(allPDFUploadResponse.UploadedPDFResponse[0].PDFDocument); httpResponseMessage.Content = new StreamContent(stream); httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "test.pdf" }; httpResponseMessage.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); httpResponseMessage.Content.Headers.ContentDisposition.FileName = "fileNameOfYourChoice.pdf"; } } catch (Exception Ex) { incidentManagementGeneralRespnse.Success = false; incidentManagementGeneralRespnse.IsException = true; incidentManagementGeneralRespnse.Message = Ex.Message; CommonFunctions.LogError(Ex); httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, incidentManagementGeneralRespnse); } return(httpResponseMessage); }
public async Task <HttpResponseMessage> UploadPDFFiles() { httpResponseMessage = new HttpResponseMessage(); allPDFUploadResponse = new AllPDFUploadResponse(); string fileName = string.Empty; try { if (Request.Content.IsMimeMultipartContent()) { var uloadPath = ConfigurationManager.AppSettings["UploadPDF"]; 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 name = p.Value; if (name.StartsWith("\"") && name.EndsWith("\"")) { name = name.Substring(1, name.Length - 2); } string value = await file.ReadAsStringAsync(); attributes.Add(name, value); } } } attributes.Add("PDFDocument", uloadPath + fileName); var json = JsonConvert.SerializeObject(attributes); var pdfFile = JsonConvert.SerializeObject(files); allPDFUploadResponse = await _IIncidentManagementService.UploadPDFFiles(json, pdfFile); httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, allPDFUploadResponse); } } catch (Exception Ex) { incidentManagementGeneralRespnse.Success = false; incidentManagementGeneralRespnse.IsException = true; incidentManagementGeneralRespnse.Message = Ex.Message; httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, incidentManagementGeneralRespnse); CommonFunctions.LogError(Ex); } return(httpResponseMessage); }