Skip to content

Commit 6af5a52

Browse files
SaravanaPriya31SaravanaPriya31
authored andcommitted
commit
1 parent 763cd43 commit 6af5a52

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

Save and Load/Save PDF file to server/Web Service/PdfViewerController.cs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.Extensions.Caching.Memory;
44
using Newtonsoft.Json;
5+
using SkiaSharp;
56
using Syncfusion.EJ2.PdfViewer;
67
using System;
78
using System.Collections.Generic;
@@ -38,27 +39,28 @@ public PdfViewerController(IWebHostEnvironment hostingEnvironment, IMemoryCache
3839
if (jsonObject != null && jsonObject.ContainsKey("document"))
3940
{
4041
if (bool.Parse(jsonObject["isFileName"]))
41-
{
42+
{
4243
string documentPath = GetDocumentPath(jsonObject["document"]);
4344
if (!string.IsNullOrEmpty(documentPath))
4445
{
4546
byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
4647
stream = new MemoryStream(bytes);
4748
}
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];
5452

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+
}
5659

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+
}
6264
}
6365
}
6466
else
@@ -240,7 +242,25 @@ public IActionResult ImportFormFields([FromBody] Dictionary<string, string> json
240242
{
241243
//Initialize the PDF Viewer object with memory cache object
242244
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+
}
244264
return Content(documentBase);
245265
}
246266

0 commit comments

Comments
 (0)