【问题标题】:How to get the file extension or content type from file byte array. in C# [duplicate]如何从文件字节数组中获取文件扩展名或内容类型。在 C# [重复]
【发布时间】:2017-01-08 04:19:24
【问题描述】:
如何从文件字节数组中获取文件扩展名或内容类型。在 C# 中
我们的生产环境中存在问题,在上次生产升级后,之前上传的 PNG 文件被保存为 JPG 文件扩展名。因此,当我读取该文件时,我无法将 mimetype 设置为 PNG,因为目前它取自 path.getextension,但这是错误的 - 我的实际文件是 PNG 类型。这会导致渲染图像出现问题。
如何从文件字节数组中获取文件扩展名或内容类型。在 C# 中
【问题讨论】:
标签:
c#
file
mime-types
content-type
【解决方案1】:
您可以使用Image.RawFormat。
if (ImageFormat.Jpeg.Equals(img.RawFormat))
{
// it is JPEG
}
else if (ImageFormat.Png.Equals(img.RawFormat))
{
// it is PNG
}
else if (ImageFormat.Gif.Equals(img.RawFormat))
{
// it is GIF
}