Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SaravanaPriya31 authored and SaravanaPriya31 committed Aug 15, 2023
1 parent 763cd43 commit 6af5a52
Showing 1 changed file with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -240,7 +242,25 @@ public IActionResult ImportFormFields([FromBody] Dictionary<string, string> 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);
}

Expand Down

0 comments on commit 6af5a52

Please sign in to comment.