diff --git a/Save and Load/Save PDF file to server/Web Service/PdfViewerController.cs b/Save and Load/Save PDF file to server/Web Service/PdfViewerController.cs index 08dd525..94419fd 100644 --- a/Save and Load/Save PDF file to server/Web Service/PdfViewerController.cs +++ b/Save and Load/Save PDF file to server/Web Service/PdfViewerController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using Newtonsoft.Json; +using SkiaSharp; using Syncfusion.EJ2.PdfViewer; using System; using System.Collections.Generic; @@ -38,27 +39,28 @@ public PdfViewerController(IWebHostEnvironment hostingEnvironment, IMemoryCache if (jsonObject != null && jsonObject.ContainsKey("document")) { if (bool.Parse(jsonObject["isFileName"])) - { + { string documentPath = GetDocumentPath(jsonObject["document"]); if (!string.IsNullOrEmpty(documentPath)) { byte[] bytes = System.IO.File.ReadAllBytes(documentPath); stream = new MemoryStream(bytes); } - string documentName = jsonObject["document"]; - string result = Path.GetFileNameWithoutExtension(documentName); - string fileName = result + "_downloaded.pdf"; - - // Save the file on the server Replace the path you want to save the document - string serverFilePath = @"wwwroot/Data/"; + else + { + string fileName = jsonObject["document"].Split(new string[] { "://" }, StringSplitOptions.None)[0]; - string filePath = Path.Combine(serverFilePath, fileName); + if (fileName == "http" || fileName == "https") + { + WebClient WebClient = new WebClient(); + byte[] pdfDoc = WebClient.DownloadData(jsonObject["document"]); + stream = new MemoryStream(pdfDoc); + } - using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) - { - //Saving the new file in root path of application - stream.CopyTo(fileStream); - fileStream.Close(); + else + { + return this.Content(jsonObject["document"] + " is not found"); + } } } else @@ -240,7 +242,25 @@ public IActionResult ImportFormFields([FromBody] Dictionary json { //Initialize the PDF Viewer object with memory cache object PdfRenderer pdfviewer = new PdfRenderer(_cache); - string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject); + string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject); + MemoryStream stream = new MemoryStream(); + + string documentName = jsonObject["documentId"]; + string result = Path.GetFileNameWithoutExtension(documentName); + string fileName = result + "_downloaded.pdf"; + + // Save the file on the server + // replace your file path to save the file in the desired location + string serverFilePath = @"wwwroot/Data/"; + + string filePath = Path.Combine(serverFilePath, fileName); + + using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) + { + //Saving the new file in root path of application + stream.CopyTo(fileStream); + fileStream.Close(); + } return Content(documentBase); }