代码之家  ›  专栏  ›  技术社区  ›  Rich Blumer

在ASP.NET中从SQL Server渲染图像。NET MVC

  •  0
  • Rich Blumer  · 技术社区  · 16 年前

    我正试图从SQL Server检索一个图像(是的,数据类型是图像),并在ASP的视图中显示它。NET MVC应用程序。有人知道如何做到这一点吗?一个工作示例将不胜感激。

    2 回复  |  直到 16 年前
        1
  •  3
  •   Matt Kocaj    16 年前

        //
        // GET: /FileManager/GetFile/ID
        //[CompressFilter(Order = 1)]
        [OutputCache(Order = 2, Duration = 600, VaryByParam = "ID")]
        //[OutputCache(Duration = 600, VaryByParam = "ID", Location = System.Web.UI.OutputCacheLocation.ServerAndClient)]
        public ActionResult GetFile(int ID)
        {
            FileService svc = new FileService(new SqlFileRepository(base.ConnectionString));
    
            KsisOnline.Data.File result = svc.GetFileByID(ID);
    
            return File(result.Data, result.MimeType, result.UploadFileName);
        }
    

    ..正如您在评论中看到的,到目前为止,我最大的问题是缓存图像结果。(顺便说一句:方法被调用 GetFile 因为它返回图像、PDF和其他文件)

        public static string Image(this HtmlHelper helper,
            string classText, string sourcePath, string altText, string width, string height)
        {
            return Image(helper, classText, sourcePath, altText, width, height, null);
        }
    
        public static string Image(this HtmlHelper helper,
            string classText, string sourcePath, string altText, string width, string height, object htmlAttributes)
        {
            StringBuilder sb = new StringBuilder();
            if (htmlAttributes != null)
                foreach (PropertyInfo p in htmlAttributes.GetType().GetProperties())
                    sb.AppendFormat(@" {0}=""{1}""", p.Name, p.GetValue(htmlAttributes, null).ToString());
    
            if (htmlAttributes == null)
                return String.Format(@"<img{0} src=""{1}"" alt=""{2}"" width=""{3}"" height=""{4}"" />",
                    String.IsNullOrEmpty(classText) ? String.Empty : String.Format(@" class=""{0}""", classText),
                    (new UrlHelper(helper.ViewContext.RequestContext)).Content(sourcePath),
                    altText, width, height);
            else
                return String.Format(@"<img{0} src=""{1}"" alt=""{2}"" width=""{3}"" height=""{4}""{5} />",
                    String.IsNullOrEmpty(classText) ? String.Empty : String.Format(@" class=""{0}""", classText),
                    (new UrlHelper(helper.ViewContext.RequestContext)).Content(sourcePath),
                    altText, width, height, sb.ToString());
        }
    
        public static string DBImage(this HtmlHelper helper,
          string classText, int fileID, string altText, string width, string height)
        {
            return DBImage(helper, classText, fileID, altText, width, height, null);
        }
    
        public static string DBImage(this HtmlHelper helper,
            string classText, int fileID, string altText, string width, string height, object htmlAttributes)
        {
            return Image(helper, classText, @"/FileManager/GetFile/" + fileID.ToString(),
                altText, width, height, htmlAttributes);
        }
    

    DBImage 是那些使用

        2
  •  1
  •   James Avery    16 年前

    创建自定义结果是处理此问题最干净的方法,这里有一个很好的例子:

    http://blog.maartenballiauw.be/post/2008/05/ASPNET-MVC-custom-ActionResult.aspx