How to Detect a File Format and Check if the File is Encrypted with JavaScript via C++
Contents
[
Hide
]
Sometimes you need to detect a file’s format before opening it because the file extension does not guarantee that the file’s content is appropriate. The file might be encrypted (a password‑protected file), so it can’t be read directly, or it should not be read. Aspose.Cells for JavaScript via C++ provides the FileFormatUtil.detectFileFormat(Uint8Array) static method and some relevant APIs that you can use to process documents.
The following sample code illustrates how to detect a file format (using the file path) and check its extension. You can also determine whether the file is encrypted.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells File Format Detection Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Detect file format
const info = AsposeCells.FileFormatUtil.detectFileFormat(new Uint8Array(arrayBuffer));
// Gets the detected load format (converted getter -> property)
const extension = AsposeCells.FileFormatUtil.loadFormatToExtension(info.loadFormat);
const encrypted = info.isEncrypted;
console.log("The spreadsheet format is: " + extension);
console.log("The file is encrypted: " + encrypted);
resultDiv.innerHTML = `<p>The spreadsheet format is: <strong>${extension}</strong></p>
<p>The file is encrypted: <strong>${encrypted}</strong></p>`;
});
</script>
</html>