Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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.

C#
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...

C#
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; }
    }
}
Posted
Updated 30-Oct-15 5:30am
v2
Comments
Foothill 30-Oct-15 11:29am    
My first guess is here

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

I think that Linq is returning and array of AdvertisementDTO objects and not the image byte array.

Well, you've got a bunch of problems. First, your code never saves the image to the database. It saves all the image metadata that you created, but the image data itself is never saved to the database. You create a byte array the length of the file content, but never put that content in the byte array.

<pre lang="cs">byte[] file = (from f in db.TBL_ADVERTISEMENT
       where f.ID == id
       select new AdvertisementDTO
       {
           DocImage = f.DOCUMENT_IMG
       }
       ).ToArray();

The error message is very clear about the the problem is. You're converting your data structure to a byte array, not the file content in the DocImage field of the AdvertisementDTO object.
 
Share this answer
 
You need something like this

C#
public byte[] DownloadImage(int id)
{
    var dto = (from f in db.TBL_ADVERTISEMENT
       where f.ID == id
       select new AdvertisementDTO
       {
           DocImage = f.DOCUMENT_IMG
       }
       ).FirstOrDefault();

     if (dto == null)
     {
         return null;
     }
     else
     {
         return dto.DocImage;
     }
}


as mentioned already though, you're not actually saving the image in the first place, so that might solve your compilation issue but not your overall problem.
 
Share this answer
 
Comments
Member 10740412 31-Oct-15 2:43am    
how can i save image data in the database?????????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900