This is my code for Download a File in my MVC App. Im having a weird error and I cant a way to put it right. The error is > Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.FileResult'
What I want with this code is, that while theres a file download it, but if theres not file(null) then it just dont do nothing or dont return nothing
public FileResult Download(string Doc)
{
string fullPath = Path.Combine(Server.MapPath("~/UploadFiles/"), Doc);
if (fullPath == null)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, Doc);
}
return RedirectToAction("List");
}
Change your action method return type to ActionResult
Both RedirectResult
and FileResult
are inheriting from this ActionResult
abstract class.
Also your if condition does not makes sense! It will always be false
as the Path.Combine will give you a non null string value. May be you want to check the file exist in the disk ?
public ActionResult Download(string Doc)
{
string fullPath = Path.Combine(Server.MapPath("~/UploadFiles/"), Doc);
if (System.IO.File.Exists(fullPath))
{
byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, Doc);
}
return RedirectToAction("List");
}
Your code should be compilable now.