Skip to content

Commit 6d78e74

Browse files
committed
feat(PdfReader): getFileSize support stream
1 parent 70f8dff commit 6d78e74

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ const loadPdf = async pdf => {
155155
pdfViewer.setDocument(pdfDocument);
156156

157157
pdfDocument.getMetadata().then(metadata => {
158+
if (metadata.contentLength === null) {
159+
metadata.contentLength = options.data.length;
160+
}
158161
loadMetadata(el, pdfViewer, metadata);
159162
});
160163

@@ -293,18 +296,24 @@ function parsePdfDate(pdfDateString) {
293296

294297
const getFilesize = metadata => {
295298
const length = metadata.contentLength;
299+
let val = 0;
300+
let unit = 'B';
296301
if (length < 1024) {
297-
return `${Math.round(length)}B`;
302+
val = length;
298303
}
299304
else if (length < 1024 * 1024) {
300-
return `${Math.round(length / 1024)}KB`;
305+
unit = 'KB';
306+
val = length / 1024;
301307
}
302308
else if (length < 1024 * 1024 * 1024) {
303-
return `${length / 1024 / 1024}MB`;
309+
unit = 'MB';
310+
val = length / 1024 / 1024;
304311
}
305312
else if (length < 1024 * 1024 * 1024 * 1024) {
306-
return `${length / 1024 / 1024 / 1024}GB`;
313+
unit = 'GB';
314+
val = length / 1024 / 1024 / 1024;
307315
}
316+
return `${Math.round(val * 100) / 100}${unit}`;
308317
}
309318

310319
const setObserver = el => {

0 commit comments

Comments
 (0)