如何使用二进制数据识别文件/ MIME类型 [英] How to identify file/MIME type using Binary data

查看:76
本文介绍了如何使用二进制数据识别文件/ MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0x1F8B0800000000000400EDBD0.......9AD6474F0000





这是保存的二进制数据数据库。它实际上是一个文件,我不知道它是哪种类型的文件。

我尝试通过writeallbytes到gz格式,因为它以0x1F开头,但是失败了。

当我以二进制格式保存gz文件时,它保存并可以成功检索。

那么我怎么知道它是什么文件,以便我可以将字节写入该格式?



This is the binary data saved in database. It is a file actually and I don't know which type of file it is.
I tried via writeallbytes to gz format as its started with 0x1F,but failed.
When I save a gz file in binary format,it saved and can be retrieved successfully.
So how can I know what file it is,so that I can write the bytes to that format?

推荐答案

当您将文件保存在数据库中时,它通常以字节(二进制)格式保存,您可以使用File.WriteAllBytes()方法将其保存回文件,它会创建一个新文件,将指定的字节数组写入文件,然后关闭该文件。如果目标文件已经存在,则会被覆盖。



见下面的代码片段

When you save file in database it normally save in Byte(binary) format, you can save it back to file using File.WriteAllBytes() method, it Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.

see below snippet
//read column value from database
//store it in ByteArray
//and then write ByteCode to file
Byte[] objByte = (Byte)dr["col1"];
System.IO.File.WriteAllBytes("D:\\test.jpg",objByte);



在上面的例子中我把文件格式作为JPG因为我知道我在数据库中保存的文件是.JPG,你应该知道文件从数据库检索frile之前的扩展/格式


In above example I have take file format as JPG cause I know the file which I have saved in database is .JPG, you should know the file extension/format before retrieve frile from database


这篇关于如何使用二进制数据识别文件/ MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆