|
2 | 2 | using Microsoft.AspNetCore.Mvc; |
3 | 3 | using Microsoft.Extensions.Caching.Memory; |
4 | 4 | using Newtonsoft.Json; |
| 5 | +using SkiaSharp; |
5 | 6 | using Syncfusion.EJ2.PdfViewer; |
6 | 7 | using System; |
7 | 8 | using System.Collections.Generic; |
@@ -38,27 +39,28 @@ public PdfViewerController(IWebHostEnvironment hostingEnvironment, IMemoryCache |
38 | 39 | if (jsonObject != null && jsonObject.ContainsKey("document")) |
39 | 40 | { |
40 | 41 | if (bool.Parse(jsonObject["isFileName"])) |
41 | | - { |
| 42 | + { |
42 | 43 | string documentPath = GetDocumentPath(jsonObject["document"]); |
43 | 44 | if (!string.IsNullOrEmpty(documentPath)) |
44 | 45 | { |
45 | 46 | byte[] bytes = System.IO.File.ReadAllBytes(documentPath); |
46 | 47 | stream = new MemoryStream(bytes); |
47 | 48 | } |
48 | | - string documentName = jsonObject["document"]; |
49 | | - string result = Path.GetFileNameWithoutExtension(documentName); |
50 | | - string fileName = result + "_downloaded.pdf"; |
51 | | - |
52 | | - // Save the file on the server Replace the path you want to save the document |
53 | | - string serverFilePath = @"wwwroot/Data/"; |
| 49 | + else |
| 50 | + { |
| 51 | + string fileName = jsonObject["document"].Split(new string[] { "://" }, StringSplitOptions.None)[0]; |
54 | 52 |
|
55 | | - string filePath = Path.Combine(serverFilePath, fileName); |
| 53 | + if (fileName == "http" || fileName == "https") |
| 54 | + { |
| 55 | + WebClient WebClient = new WebClient(); |
| 56 | + byte[] pdfDoc = WebClient.DownloadData(jsonObject["document"]); |
| 57 | + stream = new MemoryStream(pdfDoc); |
| 58 | + } |
56 | 59 |
|
57 | | - using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) |
58 | | - { |
59 | | - //Saving the new file in root path of application |
60 | | - stream.CopyTo(fileStream); |
61 | | - fileStream.Close(); |
| 60 | + else |
| 61 | + { |
| 62 | + return this.Content(jsonObject["document"] + " is not found"); |
| 63 | + } |
62 | 64 | } |
63 | 65 | } |
64 | 66 | else |
@@ -240,7 +242,25 @@ public IActionResult ImportFormFields([FromBody] Dictionary<string, string> json |
240 | 242 | { |
241 | 243 | //Initialize the PDF Viewer object with memory cache object |
242 | 244 | PdfRenderer pdfviewer = new PdfRenderer(_cache); |
243 | | - string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject); |
| 245 | + string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject); |
| 246 | + MemoryStream stream = new MemoryStream(); |
| 247 | + |
| 248 | + string documentName = jsonObject["documentId"]; |
| 249 | + string result = Path.GetFileNameWithoutExtension(documentName); |
| 250 | + string fileName = result + "_downloaded.pdf"; |
| 251 | + |
| 252 | + // Save the file on the server |
| 253 | + // replace your file path to save the file in the desired location |
| 254 | + string serverFilePath = @"wwwroot/Data/"; |
| 255 | + |
| 256 | + string filePath = Path.Combine(serverFilePath, fileName); |
| 257 | + |
| 258 | + using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) |
| 259 | + { |
| 260 | + //Saving the new file in root path of application |
| 261 | + stream.CopyTo(fileStream); |
| 262 | + fileStream.Close(); |
| 263 | + } |
244 | 264 | return Content(documentBase); |
245 | 265 | } |
246 | 266 |
|
|
0 commit comments