<legend id='JhoFG'><style id='JhoFG'><dir id='JhoFG'><q id='JhoFG'></q></dir></style></legend>

    <bdo id='JhoFG'></bdo><ul id='JhoFG'></ul>
      <i id='JhoFG'><tr id='JhoFG'><dt id='JhoFG'><q id='JhoFG'><span id='JhoFG'><b id='JhoFG'><form id='JhoFG'><ins id='JhoFG'></ins><ul id='JhoFG'></ul><sub id='JhoFG'></sub></form><legend id='JhoFG'></legend><bdo id='JhoFG'><pre id='JhoFG'><center id='JhoFG'></center></pre></bdo></b><th id='JhoFG'></th></span></q></dt></tr></i><div id='JhoFG'><tfoot id='JhoFG'></tfoot><dl id='JhoFG'><fieldset id='JhoFG'></fieldset></dl></div>

        <small id='JhoFG'></small><noframes id='JhoFG'>

      1. <tfoot id='JhoFG'></tfoot>

        ASP.NET Core MVC,从数据库中获取文件,并渲染为图像

        ASP.NET Core MVC, Get file from database, and render as image(ASP.NET Core MVC,从数据库中获取文件,并渲染为图像)

      2. <tfoot id='KrUWM'></tfoot>

          • <i id='KrUWM'><tr id='KrUWM'><dt id='KrUWM'><q id='KrUWM'><span id='KrUWM'><b id='KrUWM'><form id='KrUWM'><ins id='KrUWM'></ins><ul id='KrUWM'></ul><sub id='KrUWM'></sub></form><legend id='KrUWM'></legend><bdo id='KrUWM'><pre id='KrUWM'><center id='KrUWM'></center></pre></bdo></b><th id='KrUWM'></th></span></q></dt></tr></i><div id='KrUWM'><tfoot id='KrUWM'></tfoot><dl id='KrUWM'><fieldset id='KrUWM'></fieldset></dl></div>
              <bdo id='KrUWM'></bdo><ul id='KrUWM'></ul>

              <legend id='KrUWM'><style id='KrUWM'><dir id='KrUWM'><q id='KrUWM'></q></dir></style></legend>
                  <tbody id='KrUWM'></tbody>

                <small id='KrUWM'></small><noframes id='KrUWM'>

                1. 本文介绍了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.

                  这是我的文件模型:

                  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);
                      }
                  }
                  

                  我用这种语法来称呼它:

                  I call this with this kind of syntax:

                  <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,从数据库中获取文件,并渲染为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding DbContextOptions in Startup.cs not registering data store(在 Startup.cs 中添加 DbContextOptions 未注册数据存储)
                  Migrate html helpers to ASP.NET Core(将 html 帮助程序迁移到 ASP.NET Core)
                  Conditional validation in MVC.NET Core (RequiredIf)(MVC.NET Core 中的条件验证(RequiredIf))
                  Is it possible to serve static files from outside the wwwroot folder?(是否可以从 wwwroot 文件夹外部提供静态文件?)
                  Working with multiple resultset in .net core(在 .net 核心中使用多个结果集)
                  Where all types for http headers gone in ASP.NET 5?(ASP.NET 5 中所有类型的 http 标头都去了哪里?)

                  <i id='KVOKz'><tr id='KVOKz'><dt id='KVOKz'><q id='KVOKz'><span id='KVOKz'><b id='KVOKz'><form id='KVOKz'><ins id='KVOKz'></ins><ul id='KVOKz'></ul><sub id='KVOKz'></sub></form><legend id='KVOKz'></legend><bdo id='KVOKz'><pre id='KVOKz'><center id='KVOKz'></center></pre></bdo></b><th id='KVOKz'></th></span></q></dt></tr></i><div id='KVOKz'><tfoot id='KVOKz'></tfoot><dl id='KVOKz'><fieldset id='KVOKz'></fieldset></dl></div>
                2. <tfoot id='KVOKz'></tfoot>

                          <bdo id='KVOKz'></bdo><ul id='KVOKz'></ul>

                          1. <legend id='KVOKz'><style id='KVOKz'><dir id='KVOKz'><q id='KVOKz'></q></dir></style></legend>
                              <tbody id='KVOKz'></tbody>

                            <small id='KVOKz'></small><noframes id='KVOKz'>