Created
November 20, 2019 18:48
Star
You must be signed in to star a gist
ASP.NET Core MVC Document Viewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
ViewData["Title"] = "Home Page"; | |
} | |
<script src="http://code.jquery.com/jquery-1.8.2.js"></script> | |
<script> | |
function ViewDocument(file) { | |
$("#loader").fadeIn(); | |
var data = { FileName: file }; | |
$.ajax({ | |
type: "POST", | |
url: '/Home/OnPost', | |
data: data, | |
dataType: "text" | |
}).done(function (data) { | |
var folderName = file.replace(".", "_"); | |
$("#content").empty(); | |
for (var i = 1; i <= data; i++) { | |
$("#content").append("<img src='Content/" + folderName + "/page-" + i + ".png'/>"); | |
} | |
$("#loader").fadeOut(); | |
}) | |
} | |
</script> | |
<script type="text/javascript"> | |
$(window).load(function () { | |
$("#loader").fadeOut(1000); | |
}); | |
</script> | |
<div class="row"> | |
<div class="col-md-3"> | |
<div class="sidenav"> | |
<div id="loader"></div> | |
<h2 style="padding-left:15px">Files</h2> | |
@if (ViewBag.lstFiles != null) | |
{ | |
@foreach (string file in ViewBag.lstFiles) | |
{ | |
<a href="#" onclick="ViewDocument('@file')">@file</a> | |
} | |
} | |
</div> | |
</div> | |
<div class="col-md-9"> | |
<h2>Preview</h2> | |
<div id="content"></div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller | |
{ | |
private readonly IHostingEnvironment _hostingEnvironment; | |
private string projectRootPath; | |
private string outputPath; | |
private string storagePath; | |
List<string> lstFiles; | |
public HomeController(IHostingEnvironment hostingEnvironment) | |
{ | |
_hostingEnvironment = hostingEnvironment; | |
projectRootPath = _hostingEnvironment.ContentRootPath; | |
outputPath = Path.Combine(projectRootPath, "wwwroot/Content"); | |
storagePath = Path.Combine(projectRootPath, "storage"); | |
lstFiles = new List<string>(); | |
} | |
public IActionResult Index() | |
{ | |
var files = Directory.GetFiles(storagePath); | |
foreach (var file in files) | |
{ | |
lstFiles.Add(Path.GetFileName(file)); | |
} | |
ViewBag.lstFiles = lstFiles; | |
return View(); | |
} | |
[HttpPost] | |
public IActionResult OnPost(string FileName) | |
{ | |
int pageCount = 0; | |
string imageFilesFolder = Path.Combine(outputPath, Path.GetFileName(FileName).Replace(".", "_")); | |
if (!Directory.Exists(imageFilesFolder)) | |
{ | |
Directory.CreateDirectory(imageFilesFolder); | |
} | |
string imageFilesPath = Path.Combine(imageFilesFolder, "page-{0}.png"); | |
using (Viewer viewer = new Viewer(Path.Combine(storagePath, FileName))) | |
{ | |
//Get document info | |
ViewInfo info = viewer.GetViewInfo(ViewInfoOptions.ForPngView(false)); | |
pageCount = info.Pages.Count; | |
//Set options and render document | |
PngViewOptions options = new PngViewOptions(imageFilesPath); | |
viewer.View(options); | |
} | |
return new JsonResult(pageCount); | |
} | |
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | |
public IActionResult Error() | |
{ | |
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment