简体   繁体   English

如何获取数组缓冲区对象的 MIME 类型?

[英]How to get mime type of an array buffer object?

I am working on an API which is fetching file data as arraybuffer type.我正在开发一个 API,它以arraybuffer类型获取文件数据。 What my main question is there any of knowing what is the mime type of the file from the arraybuffer as I need to convert it to BLOB to view the file.我的主要问题是有任何知道来自arraybuffer的文件的 mime 类型是什么,因为我需要将其转换为BLOB以查看文件。

var file1 = new Blob([response], { type: file.type });
var fileURL = URL.createObjectURL(file1);

In the above code snippet, the response is an arraybuffer and the mime type I got is by storing it while uploading in a variable.在上面的代码片段中, response是一个arraybuffer ,我得到的 mime 类型是在上传时将其存储在一个变量中。 But now the scenario is I don't have any mime type in my hand so I need a way to get it.但现在的情况是我手上没有任何 mime 类型,所以我需要一种方法来获得它。

This is my response header:这是我的响应头: 标题

And this is my response body sample:这是我的响应正文示例: 身体

Can anyone suggest me anything ?任何人都可以给我建议吗?

I found this package below which is very interesting.我在下面找到了这个非常有趣的包。 It parses the array buffer/blob/stream and try to guess its extension and mime type if it were a file.它解析数组缓冲区/blob/stream 并尝试猜测它的扩展名和 mime 类型(如果它是一个文件)。 Obviously it is not exhaustive but you can find the extension list in the readme.显然它并不详尽,但您可以在自述文件中找到扩展列表。 Works on node and browser.适用于节点和浏览器。 Works for my needs though.不过适合我的需要。

Here is the repo link : https://github.com/sindresorhus/file-type这是回购链接: https : //github.com/sindresorhus/file-type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM