File size: 3,987 Bytes
56d19aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API Response Display</title>
    <style>

        body {

            font-family: Arial, sans-serif;

            margin: 20px;

        }

        .page {

            margin-bottom: 40px;

        }

        .page-number {

            font-weight: bold;

            margin-bottom: 10px;

        }

        .iframe-container {

            margin-bottom: 20px;

        }

        .download-link {

            display: block;

            margin-top: 10px;

        }

        .loading {

            display: none;

            font-weight: bold;

            color: #333;

        }

        .error {

            display: none;

            color: red;

        }

    </style>
</head>
<body>
    <h1>API Response Display</h1>
    <div id="content"></div>
    <div id="loading" class="loading">Loading...</div>
    <div id="error" class="error"></div>
    <input type="file" id="fileInput" style="display: none;">
    <button id="uploadButton">Upload File</button>

    <script>

        document.getElementById('uploadButton').addEventListener('click', () => {

            document.getElementById('fileInput').click();

        });



        document.getElementById('fileInput').addEventListener('change', async () => {

            const file = document.getElementById('fileInput').files[0];

            if (!file) return;



            const formData = new FormData();

            formData.append('file', file);



            document.getElementById('loading').style.display = 'block';

            document.getElementById('error').style.display = 'none';



            try {

                const response = await fetch('/uploadfile/', {

                    method: 'POST',

                    body: formData

                });



                if (!response.ok) {

                    throw new Error(`HTTP error! status: ${response.status}`);

                }



                const data = await response.json();

                renderPages(data);

            } catch (error) {

                console.error('Error fetching API response:', error);

                document.getElementById('error').textContent = 'An error occurred while fetching the data. Please try again.';

                document.getElementById('error').style.display = 'block';

            } finally {

                document.getElementById('loading').style.display = 'none';

            }

        });



        function renderPages(data) {

            const contentDiv = document.getElementById('content');

            contentDiv.innerHTML = ''; // Clear any existing content



            data.forEach(page => {

                const pageDiv = document.createElement('div');

                pageDiv.className = 'page';



                const pageNumberDiv = document.createElement('div');

                pageNumberDiv.className = 'page-number';

                pageNumberDiv.textContent = `Page ${page.page_number}`;



                const iframeContainer = document.createElement('div');

                iframeContainer.className = 'iframe-container';



                const iframe = document.createElement('iframe');

                iframe.src = page.html.match(/src="([^"]+)"/)[1];

                iframe.width = '100%';

                iframe.height = '600px';



                const downloadLink = document.createElement('div');

                downloadLink.className = 'download-link';

                downloadLink.innerHTML = page.download_link;



                iframeContainer.appendChild(iframe);

                pageDiv.appendChild(pageNumberDiv);

                pageDiv.appendChild(iframeContainer);

                pageDiv.appendChild(downloadLink);

                contentDiv.appendChild(pageDiv);

            });

        }

    </script>
</body>
</html>