如何在ASP.NET MVC 4中以转换varbinary(max)的方式显示图像,从SQL Server转换为图像。

1 人关注

我的图像存储格式是:文件 -> byte[] -> string base64 -> varbinary(max)

我的图像检索格式是 varbinary(max) -> byte[] -> 然后是图像格式。

但我不能显示图像。

请帮助检索图像格式。

2 个评论
在存储过程中放弃 string base64 这个步骤--你在检索时没有这样做,如果你存储在 Varbinary(max) ,这也是完全没有必要和没有意义的。
谢谢Marc,我放弃了基数为64的字符串,哪一步是检索图像格式的正确方法?
asp.net-mvc
sql-server-2008
asp.net-mvc-4
razor
Mano Johnbritto
Mano Johnbritto
发布于 2015-07-25
1 个回答
sp7
sp7
发布于 2015-07-25
已采纳
0 人赞同

你的图像的mime类型是什么? 也许你需要一种方法 Controller.File(fileContents: yourByteArray, contentType: "image/jpeg")

// Represent a image.
public class MyImage
    public ID { get; set; }
    public byte[] Data { get; set; }
    public string MimeType { get; set; } 

上传图片。

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
    if (ModelState.IsValid)
        if (file != null)
            var image = new MyImage() 
                MimeType = file.ContentType,
                Data = new byte[file.ContentLength]
            file.InputStream.Read(image.Data, 0, file.ContentLength); 
        repository.SaveImage(image);
        return RedirectToAction("Index");
        return View(product);

获取图片。

public FileContentResult GetImage(int imageID)
    var image = repository.Images.FirstOrDefault(p => p.ID == imageID);
    if (image != null)
         return File(image.Data, image.MimeType); 
       return null;

查看:显示图片

@model YourApp.Entities.MyImage
<div class="item">