Returning a file to View/Download in ASP.NET MVC

前端 未结 9 1555
刺人心
刺人心 2020-11-21 22:41

I\'m encountering a problem sending files stored in a database back to the user in ASP.NET MVC. What I want is a view listing two links, one to view the file and let the mim

9条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 22:46

    I believe this answer is cleaner, (based on https://stackoverflow.com/a/3007668/550975)

        public ActionResult GetAttachment(long id)
        {
            FileAttachment attachment;
            using (var db = new TheContext())
            {
                attachment = db.FileAttachments.FirstOrDefault(x => x.Id == id);
            }
    
            return File(attachment.FileData, "application/force-download", Path.GetFileName(attachment.FileName));
        }
    

提交回复
热议问题