文件作为json属性如何获取它

时间:2016-05-18 06:58:22

标签: c# json

我通过html控件加载文件(图像),并将其与其他json属性一起发送到我的API。 图片属性看起来:

Picture: "data:image/png;abase64,iVBORw0KGgoAAAANS {a lot of chars}="

在我的控制器中,我需要得到: 1.文件名 2.文件扩展名 3.将文件保存在数据库中的文件 4.宽度,高度 5.其他信息

我怎么能这样做? 我有这个:

var bytes = Convert.FromBase64String(model.Picture);
Image image;
using (var stream = new MemoryStream(bytes))
{
    image = Image.FromStream(stream);
}

但是图片没有Name,Extension的属性。

2 个答案:

答案 0 :(得分:1)

对于高度和宽度,您可以使用

var height = image.Height;
var width = image.Width;

至于图像类型,我发现这个片段很有用:

string mimeType = string.Empty;
Guid id = image.RawFormat.Guid;
if (id == ImageFormat.Png.Guid)
{
    mimeType = "image/png";
}
else if (id == ImageFormat.Bmp.Guid)
{
    mimeType = "image/bmp";
}
else if (id == ImageFormat.Emf.Guid)
{
    mimeType = "image/x-emf";
}
else if (id == ImageFormat.Exif.Guid)
{
    mimeType = "image/jpeg";
}
else if (id == ImageFormat.Gif.Guid)
{
    mimeType = "image/gif";
}
else if (id == ImageFormat.Icon.Guid)
{
    mimeType = "image/ico";
}
else if (id == ImageFormat.Jpeg.Guid)
{
    mimeType = "image/jpeg";
}
else if (id == ImageFormat.MemoryBmp.Guid)
{
    mimeType = "image/bmp";
}
else if (id == ImageFormat.Tiff.Guid)
{
    mimeType = "image/tiff";
}
else if (id == ImageFormat.Wmf.Guid)
{
    mimeType = "image/wmf";
}

答案 1 :(得分:0)

试试这个

    var bytes = Convert.FromBase64String(model.Picture);
        using (var imageToSave= new FileStream(filePath, FileMode.Create))
{
    imageToSave.Write(bytes ,0, bytes.Length);
    imageToSave.Flush();
}