Can an ASP.NET MVC controller return an Image?

前端 未结 19 1644
旧时难觅i
旧时难觅i 2020-11-22 02:10

Can I create a Controller that simply returns an image asset?

I would like to route this logic through a controller, whenever a URL such as the following is requeste

19条回答
  •  长情又很酷
    2020-11-22 02:52

    you can use File to return a file like View, Content etc

     public ActionResult PrintDocInfo(string Attachment)
                {
                    string test = Attachment;
                    if (test != string.Empty || test != "" || test != null)
                    {
                        string filename = Attachment.Split('\\').Last();
                        string filepath = Attachment;
                        byte[] filedata = System.IO.File.ReadAllBytes(Attachment);
                        string contentType = MimeMapping.GetMimeMapping(Attachment);
    
                        System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
                        {
                            FileName = filename,
                            Inline = true,
                        };
    
                        Response.AppendHeader("Content-Disposition", cd.ToString());
    
                        return File(filedata, contentType);          
                    }
                    else { return Content("

    Patient Clinical Document Not Uploaded

    "); } }

提交回复
热议问题