[英]How to get file extension from file content using Node.js
我需要一個幫助。 我需要僅使用 Node.js 從文件內容中獲取文件類型。 我正在解釋下面的場景。
可以說我有一些如下內容。
let fileContent = "The textContent property sets or returns the text content of the specified node, and all its descendants."
如果我們可以在這里看到變量fileContent
有一些文本數據,我想將這些數據寫入一個文件。 因此,為此我需要創建具有適當擴展名的文件,例如abc.txt
。 同樣,如果fileContent
有一些json
數據,那么我將創建abc.json
之類的文件並將值寫入其中。
因此,在這里我需要僅使用Node.js
從此內容中獲取文件的type
。 如果有人對此有解決方案,將有很大幫助。
為此,您可以使用流行的文件類型package。 您可以發送緩沖區/文件/流/blob 作為輸入並獲取文件類型為 output。
const FileType = require('file-type');
...
const { ext, mime } = await FileType.fromBuffer(your_data);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.