简体   繁体   中英

Error in downloading a file in ASP.NET MVC

I am working in ASP.NET MVC. I have stored a file in the database now I want to download and display its contents. I am working in layers.

Here is my code.

Controller Action used for uploading file

[HttpPost]
public ActionResult Edit(int id, UpdateAdvertisement model, HttpPostedFileBase file)
{
    try
    {
        AdvertisementDTO add = new AdvertisementDTO();                
        add.DocImage = new byte[file.ContentLength];
        add.ContentType = file.ContentType;
        add.DocName = Convert.ToString(DateTime.Now.Ticks);              
        new AdvertisementHandler().Update(id, add);
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

Controller Action for downloading file

 public FileContentResult DownloadFile(int id)
    {
        string DocumentContentType  = new AdvertisementHandler().DownloadContent(id);
        string DocumentName = new AdvertisementHandler().DownloadDocumentName(id);
        byte[] DocumentImage = new AdvertisementHandler().DownloadImage(id);
        //return File(filename, contentType, "Report.pdf");
        return File(DocumentImage, DocumentContentType, DocumentName);
        //return File.ReadAllBytes(DocumentName);
    }

Business Logic Layer

These are the queries that are used to access database.

public byte[] DownloadImage(int id)
{
    byte[] file = (from f in db.TBL_ADVERTISEMENT
       where f.ID == id
       select new AdvertisementDTO
       {
           DocImage = f.DOCUMENT_IMG
       }
       ).ToArray();
     return file;
}

public string DownloadContent(int id ) 
{
    string file = (from f in db.TBL_ADVERTISEMENT
      where f.ID == id
      select new AdvertisementDTO
      {
          ContentType = f.CONTENTTYPE
      }
      ).ToString();
     return file;
}

public string DownloadDocumentName(int id)
{
    string file = (from f in db.TBL_ADVERTISEMENT
      where f.ID == id
      select new AdvertisementDTO
      {
        DocName = f.DOC_NAME
      }
      ).ToString();
      return file;
}

This error arises when i compile this code

Error 1

Cannot implicitly convert type 'ORS.DTO.AdvertisementDTO[]' to 'byte[]'

F:\\Projects\\Online Recruitment System\\ORS.BLL\\AdvertisementHandler.cs 59 28 ORS.BLL

Here is my AdvertisementDTO...

namespace ORS.DTO
{
    public class AdvertisementDTO
    {
        public int ID { get; set; }
        public string AddNumber { get; set; }
        public string Description { get; set; }
        public byte[] DocImage { get; set; }
        public string ContentType { get; set; }
        public string DocName { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public int StatusID { get; set; }

        public virtual RecordStatusDTO RecordStatus { get; set; }
    }
}

Calling .ToArray() on an object does not convert it to a byte array. You omitted the definition of AdvertisementDTO so I can only guess that it is already a byte array. If that's not the case please post the code for AdvertisementDTO and I'll update this post.

    byte[] file = (from f in db.TBL_ADVERTISEMENT
                   where f.ID == id
                   select f.DOCUMENT_IMG).SingleOrDefault();

    return file;

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