0

大家好,我有一个 FileUpload 控件,用户可以在其中上传图像和 Doc 文件,当我从数据库中检索数据时,我现在将路径的 Url 保存在我的数据库中,我想检查文件是 .doc 还是图像是文档,它将打开文件我的问题是我如何为图像执行此操作我如何检查它的图像我必须在图像控件中显示图像从未在 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以及我必须在这里更改的代码是什么请有任何建议

4

1 回答 1

0

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

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

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-08-03T15:04:35.297 回答