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
ActionResultBoth
RedirectResultandFileResultare inheriting from thisActionResultabstract class.Also your if condition does not makes sense! It will always be
falseas the Path.Combine will give you a non null string value. May be you want to check the file exist in the disk ?Your code should be compilable now.