【问题标题】:how to Display the Image From Database and Display In View in MVC3?如何在 MVC3 中显示数据库中的图像并在视图中显示?
【发布时间】:2012-08-03 14:35:57
【问题描述】:

大家好,我有一个 FileUpload 控件,用户可以在其中上传图像和文档文件,并且我将路径的 Url 保存在我的数据库中 现在,当我从数据库中检索数据时,我想检查文件是 .doc 还是图像,如果它是 Doc,它将打开文件我的问题是我如何为 Image 执行此操作我如何检查它是否是图像而我必须在图像控件中显示图像从未在 MVC3 中的图像控件上工作过

这是我的控制器代码

       public ActionResult ViewAttachments(string AttachmentName)
    {
        try
        {
            AttachmentName = Session["AttachmentUrl"].ToString();
            var fs = System.IO.File.OpenRead(Server.MapPath(" "+ AttachmentName+" "));
            return File(fs, "application/doc", AttachmentName);
        }
        catch
        {
            throw new HttpException(404, "Couldn't find " + AttachmentName);
        }
    } 

我在这里使用一个 aspx 页面我必须使用什么 html,我必须在这里更改什么代码,请有任何建议

【问题讨论】:

    标签: asp.net-mvc-3 image


    【解决方案1】:

    您可以做的是对扩展进行简单的检查,然后返回您正在执行的文件结果或包含简单图像控件的局部视图。

    要扩展您的代码,您可以执行以下操作:

    public ActionResult ViewAttachments(string AttachmentName)
        {
            try
            {
                AttachmentName = Session["AttachmentUrl"].ToString();
                var fs = System.IO.File.OpenRead(Server.MapPath(" " + AttachmentName + " "));
                var ext = Path.GetExtension(fs.Name);
    
                switch (ext)
                {
                    case "doc":
                        return File(fs, "application/doc", AttachmentName);
                    case "jpg":
                    case "jpeg":
                    case "png":
                        return PartialView("_imgControl", "http://www.mysite.com/downloads/" + AttachmentName + ext); 
                }                
            }
            catch
            {
                throw new HttpException(404, "Couldn't find " + AttachmentName);
            }
        }
    

    然后在您的局部视图中,您可以将模型对象(在本例中为您网站上图像路径的 url)返回到标准 html 图像控件。

    【讨论】:

      猜你喜欢
      • 2012-01-18
      • 1970-01-01
      • 2013-09-21
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多