简体   繁体   中英

Download pdf using asp.net codefirst

Hi everyone so I am trying to create an application using asp.net mvc with a code first database that allows the users to be able to upload and download images so far I have got it to save to the database and display but I am having trouble with my controller to work out how to allow the user to download the pdf file.

currently in the controller there are these issues

where is underlined saying PoliciesImageModel does not contain the definition for where . and policies is underlined saying cannot convert PoliciesImageModel to byte

Thanks for anyhelp with this issue

Update I am using Matts answer but PoliciesImages and the file are not being recognised

View

 @model List<MyProject.Models.PoliciesPostVM>
 @foreach (var item in Model)
 {
 @Html.DisplayFor(modelItem => item.FileName)
 @Html.ActionLink("Download", "PoliciesDownload", new { id = item.ID})
 }

Controller

   public FileContentResult PoliciesDownload(int ID)
    {
        if (ID == 0) { return null; }
        PoliciesImageModel policies = new PoliciesImageModel();

        policies = policies.Where(a => a.ID == ID).SingleOrDefault();
        return File(policies, "application/pdf");
    }

model

 public partial class PoliciesPostModel
    {
        public PoliciesPostModel()
    {
            Pdfs = new List<PoliciesImageModel>();
    }
    [Key]
    [HiddenInput(DisplayValue = false)]
    public int ID { get; set; }
    [Required(ErrorMessage = "Heading is Required")]
    [Display(Name = "File Name")]
    public string FileName { get; set; }
    public virtual ICollection<PoliciesImageModel> Pdfs { get; set; }
    public IEnumerable<HttpPostedFileBase> File { get; set; }
}

public class PoliciesImageModel
    {
    [Key]
    public int ID { get; set; }
    public string Path { get; set; }
    public virtual PoliciesPostModel Post { get; set; }
    public string DisplayName { get; set; }
}
public class PoliciesImageVM
    {
    public int? ID { get; set; }
    public string Path { get; set; }
    public string DisplayName { get; set; }
    public bool IsDeleted { get; set; }
}
public partial class PoliciesPostVM
    {
    public PoliciesPostVM()
    {
            Pdfs = new List<PoliciesImageVM>();
    }

    public int? ID { get; set; }
    public string FileName { get; set; }
    public IEnumerable<HttpPostedFileBase> Files { get; set; }
    public List<PoliciesImageVM> Pdfs { get; set; }
    public IEnumerable<PoliciesPostModel> Posts { get; set; }   
}

DbConext

public class EFDbContext: DbContext
{
    #region Policies
    public DbSet<PoliciesPostModel> PoliciesPosts { get; set; }
    public DbSet<PoliciesImageModel> PoliciesImages { get; set; }
    #endregion Policies   
    //other Dbsets are here too....
}

You are trying to return a FileContentResult . That is just an array of bytes (and that is exactly what a PDF file is). You will need to use the Path property to search on disk and get the contents of the PDF file into a byte array. Then stream that array out instead of your PoliciesImageModel

In addition, you are creating a new, individual PoliciesImageModel object, not a list of PoliciesImageModel from the database. You'll need to get the list from the database context and do the filter on that list.

public ActionResult PoliciesDownload(int ID)
{
    if (ID == 0) { return null; }
    using (var context = new DBContext()) {
        PoliciesImages policies = context.PoliciesImages;
        var policy = policies.Where(a => a.ID == ID).SingleOrDefault();
        byte[] fileBytes = File.ReadAllBytes(policies.Path);
        Response.AddHeader("Content-Disposition", "inline; filename=policies.DisplayName + ".pdf");
        return File(fileBytes , "application/pdf");
    }
    return null;
}

This has not been tested. I'm probably missng something. But this is the general idea.

I ended up using this code it doesnt downloaded it but instead opens the pdf up in a new tab and displays it. Controller

 public ActionResult PoliciesDownload(int ID)
        {
            if (ID == 0) { return null; }
            PoliciesImageModel resume = new PoliciesImageModel();
            EFDbContext rc = new EFDbContext();
            resume = rc.PoliciesImages.Where(a => a.ID == ID).SingleOrDefault();
            return File(resume.Path, "application/pdf");
        }

View

        @Html.ActionLink("View Pdf",  "PoliciesDownload", new { id = item.ID }, new { @target = "_blank" })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM