Show epub book in browser from Folder (Directory) in ASP.Net using C# and VB.Net

mukesh1
 
on Jun 23, 2022 12:02 AM
425 Views

It's not reading from local server path

<script type="text/javascript">
    window.onload = function () {
        ePubReader("<%=epubPath%>");
        /*ePubReader("https://s3.amazonaws.com/epubjs/books/alice.epub");*/
    };
</script>

 

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string path = Server.MapPath("~/Books/Documents/25707spiderspider.epub");
        epubPath.Append(path);
    }
}

public StringBuilder epubPath = new StringBuilder();
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 23, 2022 03:10 AM

Hi mukesh1,

You need to convert the url to AbsoluteUri.

Then set the path in a property and use that in the JavaScript.

Refer below sample.

HTML

<div id="main">
    <div id="titlebar">
        <div id="metainfo">
            <span id="book-title"></span>
            <span id="title-seperator">&nbsp;&nbsp;–&nbsp;&nbsp;</span>
            <span id="chapter-title"></span>
        </div>
    </div>
    <div id="divider"></div>
    <div id="prev" class="arrow">‹</div>
    <div id="viewer"></div>
    <div id="next" class="arrow">›</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
<script type="text/javascript" src="/Scripts/epub.js"></script>
<script type="text/javascript" src="/Scripts/reader.js"></script>
<link rel="stylesheet" href="CSS/main.css">
<script type="text/javascript">
    window.onload = function () {
        ePubReader("<%=epubPath%>");
    };
</script>

Code

C#

protected string epubPath { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
    epubPath = GetUrl("alice.epub");
}

protected string GetUrl(string filePath)
{
    string[] splits = Request.Url.AbsoluteUri.Split('/');
    if (splits.Length >= 2)
    {
        string url = splits[0] + "//";
        for (int i = 2; i < splits.Length - 1; i++)
        {
            url += splits[i];
            url += "/";
        }
        return url + filePath;
    }
    return filePath;
}

VB.Net

Protected Property epubPath As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    epubPath = GetUrl("alice.epub")
End Sub

Protected Function GetUrl(ByVal filePath As String) As String
    Dim splits As String() = Request.Url.AbsoluteUri.Split("/"c)
    If splits.Length >= 2 Then
        Dim url As String = splits(0) & "//"
        For i As Integer = 2 To splits.Length - 1 - 1
            url += splits(i)
            url += "/"
        Next
        Return url & filePath
    End If
    Return filePath
End Function

You need to add the mime type th the web config inside the system.webServer section.

<system.webServer>
  <staticContent>
    <mimeMap fileExtension=".epub" mimeType="application/octet-stream" />
  </staticContent>
</system.webServer>

Screenshot