ASP.NET Core MVC,从数据库获取文件,并呈现为图像 [英] ASP.NET Core MVC, Get file from database, and render as image

查看:470
本文介绍了ASP.NET Core MVC,从数据库获取文件,并呈现为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将图像数据存储在字段类型为 varbinary(max)的SQL表中.

I have image data being stored in a SQL table with a field type of varbinary(max) I also store the image content type.

我正在使用Microsoft ASP.NET Core MVC和Dapper,尝试从数据库中获取文件并将其呈现为图像.

Using Microsoft ASP.NET Core MVC and Dapper, I am trying to get the file back from the database and render it as an image.

这是我的 FileModel :

using System;

namespace Brand.Models
{
    public class FileModel
    {
        public Guid ID { get; set; }
        public string FileName { get; set; }
        public string FileType { get; set; }
        public byte[] FileData { get; set; }
        public int FileLength { get; set; }
        public long Version { get; set; }
        public string FileSource { get; set; }
        public DateTime Created { get; set; }

        public FileModel() 
        {
            FileLength = 0;
            Version = 1;
            Created = DateTime.Now;
        }
    }
}

这些是我的功能:

public FileStreamResult GetFile(string FileID)
{
    return GetFile(new Guid(FileID));
}

public FileStreamResult GetFile(Guid FileID)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        DynamicParameters dynamicParameters = new DynamicParameters();
        dynamicParameters.Add(@"ID", FileID);
        FileModel result = connection.Query<FileModel>("GetFile", dynamicParameters, commandType: CommandType.StoredProcedure).FirstOrDefault();
        Stream stream = new MemoryStream(result.FileData);
        return new FileStreamResult(stream, result.FileType);
    }
}

我用这种语法称呼它:

<img src="@fileExtensions.GetFile(content.FileID)" />

尝试此操作时会收到404响应,因此显然我实现此操作的方式出了问题.

I am getting a 404 response when I try this, so obviously something is wrong with the way I am implementing this.

推荐答案

下面的代码只是我的头上的话题,因此可能需要一些调整,但是它将为您提供基本的思想.

The below code is just off the top of my head, so it might need some tweaks, but it will give you the basic idea.

创建一个新的控制器,并将您的方法放在其中:

Create a new controller and put your methods in there:

public ImageController : Controller
{
    public FileStreamResult GetFile(string FileID)
    {
        return GetFile(new Guid(FileID));
    }

    public FileStreamResult GetFile(Guid FileID)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            DynamicParameters dynamicParameters = new DynamicParameters();
            dynamicParameters.Add(@"ID", FileID);
            FileModel result = connection.Query<FileModel>("GetFile", dynamicParameters, commandType: CommandType.StoredProcedure).FirstOrDefault();
            Stream stream = new MemoryStream(result.FileData);
            return new FileStreamResult(stream, result.FileType);
        }
    }
}

然后在您的标记中,引用控制器:

then in your markup, reference the controller:

<img src="@Url.Action("GetFile", "Image", new {FileId = content.FileID})" />

这篇关于ASP.NET Core MVC,从数据库获取文件,并呈现为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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