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(DocumentImage, DocumentContentType, 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; }
}
}